Mega Code Archive

 
Categories / C# / XML
 

XmlReader ReadElementContentAsObject reads the current element and returns the contents as an Object

using System; using System.IO; using System.Xml; public class Sample  {   public static void Main(){           XmlReaderSettings settings = new XmlReaderSettings();           settings.ValidationType = ValidationType.Schema;           settings.Schemas.Add("urn:items", "item.xsd");             XmlReader reader = XmlReader.Create("item.xml", settings);            reader.ReadToFollowing("price");           Console.WriteLine(reader.ValueType);           Decimal price = (Decimal) reader.ReadElementContentAsObject();           price = Decimal.Add(price, 2.50m);   } } /* <item xmlns="urn:items" productID='123098'>  <name>Book</name>  <price>9.95</price>  <supplierID>2000</supplierID> </item> <?xml version="1.0"?> <xs:schema xmlns:tns="urn:items" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:items" xmlns:xs="http://www.w3.org/2001/XMLSchema">   <xs:element name="item">     <xs:complexType>       <xs:sequence>         <xs:element name="name" type="xs:string" />         <xs:element name="price" type="xs:decimal" />         <xs:element name="supplierID" type="xs:unsignedShort" />       </xs:sequence>       <xs:attribute name="productID" type="xs:unsignedInt" use="required" />     </xs:complexType>   </xs:element> </xs:schema> */