Posts

Showing posts with the label xml

Parse XML to object model in C# using XSD utility

Image
* Run xsd Visual Studio command line utility to generate object from xsd file (from Visual Studio Command Prompt) * Then add the cs file to your project (same place as your xsd file) < xsd : schema xmlns : xsd = "http://www.w3.org/2001/XMLSchema" targetNamespace = "urn:contacts" xmlns : bks = "urn:contacts" > < xsd : element name = "contacts" type = "bks:Contacts" /> < xsd : complexType name = "Contacts" > < xsd : sequence > < xsd : element name = "contact" type = "bks:Contact" minOccurs = "0" maxOccurs = "unbounded" /> </ xsd : sequence > </ xsd : complexType > < xsd : complexType name = "Contact" > < xsd : sequence > < xsd : element name = "firstName" type = "xsd:string" /> < xsd : element name ...

Parse XML to dynamic object in C#

<? xml version="1.0" encoding="utf-8" ?> < contacts > < contact id = '1' > < firstName > Michael </ firstName > < lastName > Jordan </ lastName > < age > 40 </ age > < dob > 1965 </ dob > < salary > 100.35 </ salary > </ contact > < contact id = '2' > < firstName > Scottie </ firstName > < lastName > Pippen </ lastName > < age > 38 </ age > < dob > 1967 </ dob > < salary > 55.28 </ salary > </ contact > </ contacts > public class XmlToDynamic { public static void Parse(dynamic parent, XElement node) { if (node.HasElements) { if (node.Elements(node.Elements().First().Name.LocalName).Count() > 1) { //list var item = new ExpandoObject(); var list = new List<dynamic...

Reading XML file in C# using XPath expressions

An example how to read XML file using XPath expression to access elements and attributes. Sample XML file <? xml version="1.0" encoding="utf-8" ?> < bookstore > < book genre = "autobiography" publicationdate = "1981-03-22" ISBN = "1-861003-11-0" > < title1 > The Autobiography of Benjamin Franklin </ title1 > < author > < first - name > Benjamin </ first -name > < last - name > Franklin </ last -name > </ author > < price > 8.99 </ price > </ book > < book genre = "novel" publicationdate = "1967-11-17" ISBN = "0-201-63361-2" > < title1 > The Confidence Man </ title1 > < author > < first - name > Herman </ first -name > < last - name > Melville </ last -n...