【发布时间】:2019-09-23 08:56:56
【问题描述】:
我有一个如下所示的 XML 文件,
<?xml version="1.0"?>
<MainClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<Settings xsi:type="FileModel">
<Name>FileOne</Name>
<IsActive>true</IsActive>
<IsHidden>false</IsHidden>
</Settings>
<Settings xsi:type="FileModel">
<Name>FileTwo</Name>
<IsActive>true</IsActive>
<IsHidden>false</IsHidden>
</Settings>
<Settings xsi:type="ServerModel">
<Name>DelRep</Name>
<IsActive>false</IsActive>
<IsHidden>false</IsHidden>
</Settings>
</Items>
<DirectoryPath>D:\MainFolder</DirectoryPath>
</MainClass>
我正在使用以下代码提取一些数据,
XDocument File = XDocument.Load(path);
XElement element = File .Root.Elements().Single(x => x.Name == "DirectoryPath");
string usingPath = element.Value;
我一直在尝试向上述代码添加某种验证,这样即使在 xml 文件缺少部分 <DirectoryPath>D:\MainFolder</DirectoryPath> 的情况下,我也不会收到错误“sequence contains no matching element "。
在 C# 中是否有类似于可能是 Path.Exist 的属性来验证 XML 元素的存在
【问题讨论】:
标签: c# xml serialization