C# 使用LINQ时的默认xml属性值

C# 使用LINQ时的默认xml属性值,c#,xml,xml-parsing,xsd,linq-to-xml,C#,Xml,Xml Parsing,Xsd,Linq To Xml,假设您有一个XML文件: <experiment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="experiment.xsd"> <something /> <experiment> LINQ是自动加载XML模式(从根元素“实验”的“xsi:noNamespaceSchemaLocation”属性)还是必须手动强制加载 如何

假设您有一个XML文件:

<experiment
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="experiment.xsd">
  <something />
<experiment>
LINQ是自动加载XML模式(从根元素“实验”的“xsi:noNamespaceSchemaLocation”属性)还是必须手动强制加载


如何强制LINQ读取可选属性及其默认值?

如果使用正确的XmlReader设置,则加载方法将使用XmlReader(也就是说,需要验证并将ValidationType设置为schema,并分别注意schemaLocation和具有适当ValidationFlags的noNamespaceSchemaLocation),然后我认为将创建该属性,并使用来自schema的默认值填充该属性

以下是一个简短的示例:

    XDocument doc;

    XmlReaderSettings xrs = new XmlReaderSettings();
    xrs.ValidationType = ValidationType.Schema;
    xrs.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;


    using (XmlReader xr = XmlReader.Create("../../XMLFile1.xml", xrs))
    {
        doc = XDocument.Load(xr);
    }
    foreach (XElement foo in doc.Root.Elements("foo"))
    {
        Console.WriteLine("bar: {0}", (bool)foo.Attribute("bar"));
    }
带有包含以下内容的示例文件

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XMLSchema1.xsd">
  <foo/>
  <foo bar="true"/>
  <foo bar="false"/>
</root>

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="foo">
          <xs:complexType>
            <xs:attribute name="bar" use="optional" type="xs:boolean" default="false"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
使用此XelementTextensions.cs类,可以使用采用默认值的
Get()
方法:

XDocument xml = XDocument.Load("file.xml");
bool b = xml.Descendants("something").First().Get("hello", false); 

false
是您提供的默认值。

这就是为什么您应该使用强制转换,而不是使用
Value
属性(顺便说一句,该属性返回一个字符串)。我使用bool.Parse,只是忘了把它放在那里。这感觉应该可以,但不知为什么不行。我将尝试解决它,我认为你的方法很好。我认为他没有找到架构,尽管Visual Studio找到了它。我在架构URI中有一个相对地址,这可能是个问题吗?../../experience.xsd“将ValidationEventHandler添加到您的XmlReader中,并启用标志来报告ValidationWarnings,然后,如果找不到该文件,ValidationEventHandler应将其报告为警告。感谢EvenHandler非常方便。当我昨天晚上外出时,我突然想到了错误所在。我没有设置“如果更新,请复制”到xsd文件。。。
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XMLSchema1.xsd">
  <foo/>
  <foo bar="true"/>
  <foo bar="false"/>
</root>

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="foo">
          <xs:complexType>
            <xs:attribute name="bar" use="optional" type="xs:boolean" default="false"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
bar: False
bar: True
bar: False
XDocument xml = XDocument.Load("file.xml");
bool b = xml.Descendants("something").First().Get("hello", false);