Default xml attribute value when using LINQ

SmartBoy786

New Member
Let's say you have an XML file:\[code\]<experiment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="experiment.xsd"> <something /><experiment>\[/code\]And you have the xsd file:\[code\]...<xs:attribute name="hello" type="xs:boolean" use="optional" default="false" />...\[/code\]Let's assume that the attribute "hello" is an optional attribute of the "something" element with a default value set to "false".When using XDocument of XML LINQ, the attribute is missing, causing the program to fail while trying to read it:\[code\]XDocument xml = XDocument.Load("file.xml");bool b = bool.Parse(xml.Descendants("something").First().Attribute("hello").Value); // FAIL\[/code\]Does LINQ load the XML schema automatically (from the "xsi:noNamespaceSchemaLocation" attribute of the root element "experiment") or do I have to force it manually?How to force LINQ to read the optional attributes and their default values?
 
Back
Top