假设您有一个XML文件:
<experiment
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="experiment.xsd">
<something />
<experiment>
你有xsd文件:
...
<xs:attribute name="hello" type="xs:boolean" use="optional" default="false" />
...
假设属性“hello”是“something”元素的可选属性,默认值设置为“false”。
使用XML LINQ的XDocument时,该属性丢失,导致程序在尝试读取时失败:
XDocument xml = XDocument.Load("file.xml");
bool b = bool.Parse(xml.Descendants("something").First().Attribute("hello").Value); // FAIL
LINQ是否自动加载XML模式(来自根元素“experiment”的“xsi:noNamespaceSchemaLocation”属性)还是我必须手动强制它?
如何强制LINQ读取可选属性及其默认值?
答案 0 :(得分:6)
Load
方法需要一个XmlReader,如果你使用一个正确的XmlReaderSettings http://msdn.microsoft.com/en-us/library/1xe0740a(即需要验证ValidationType设置为模式并分别关注schemaLocation noNamespaceSchemaLocation和正确的ValidationFlags)然后我认为该属性将使用模式中的默认值创建和填充。
这是一个简短的样本:
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>
输出
bar: False
bar: True
bar: False
答案 1 :(得分:1)
使用此Xml Library XElementExtensions.cs类,您可以使用采用默认值的Get()
方法:
XDocument xml = XDocument.Load("file.xml");
bool b = xml.Descendants("something").First().Get("hello", false);
false
是您提供的默认值。