Last active
January 15, 2018 18:50
-
-
Save dandohotaru/bc3766f98a81b3538a021dc573649cf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rely on http://xmltocsharp.azurewebsites.net/ for xml to C# mappings | |
void Main() | |
{ | |
var xml = @" | |
<SalesQuotes xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://api.some.com/version/1'> | |
<Pagination> | |
<NumberOfItems>2380</NumberOfItems> | |
<PageSize>200</PageSize> | |
<PageNumber>1</PageNumber> | |
<NumberOfPages>12</NumberOfPages> | |
</Pagination> | |
<SalesQuote> | |
<Guid>825634b9-28f5-4aa7-98e7-5e4a4ed6bc6a</Guid> | |
<LastModifiedOn>2018-01-09T12:23:56.6133445</LastModifiedOn> | |
<Comments>Please note: | |
installation is not included in this quote | |
</Comments> | |
</SalesQuote> | |
</SalesQuotes>"; | |
var processor = new XmlProcessor(); | |
processor.Run(xml); | |
} | |
public class XmlProcessor | |
{ | |
public void Run(string xml) | |
{ | |
// Load | |
var document = XDocument.Parse(xml); | |
var xmlns = document.Root.GetDefaultNamespace(); | |
document.Dump("Xml"); | |
// Find | |
var query = from element in document | |
.Descendants(xmlns + "SalesQuote") | |
.Elements(xmlns + "Guid") | |
where element.Value == "825634b9-28f5-4aa7-98e7-5e4a4ed6bc6a" | |
select element; | |
query.Dump("Node"); | |
} | |
} | |
[XmlRoot(ElementName = "Pagination")] | |
public class Pagination | |
{ | |
[XmlElement(ElementName = "NumberOfItems")] | |
public string NumberOfItems { get; set; } | |
[XmlElement(ElementName = "PageSize")] | |
public string PageSize { get; set; } | |
[XmlElement(ElementName = "PageNumber")] | |
public string PageNumber { get; set; } | |
[XmlElement(ElementName = "NumberOfPages")] | |
public string NumberOfPages { get; set; } | |
} | |
[XmlRoot(ElementName = "SalesQuote")] | |
public class SalesQuote | |
{ | |
[XmlElement(ElementName = "Guid")] | |
public string Guid { get; set; } | |
[XmlElement(ElementName = "LastModifiedOn")] | |
public string LastModifiedOn { get; set; } | |
[XmlElement(ElementName = "Comments")] | |
public string Comments { get; set; } | |
} | |
[XmlRoot(ElementName = "SalesQuotes")] | |
public class SalesQuotes | |
{ | |
[XmlElement(ElementName = "Pagination")] | |
public Pagination Pagination { get; set; } | |
[XmlElement(ElementName = "SalesQuote")] | |
public SalesQuote SalesQuote { get; set; } | |
[XmlAttribute(AttributeName = "xsd")] | |
public string Xsd { get; set; } | |
[XmlAttribute(AttributeName = "xsi")] | |
public string Xsi { get; set; } | |
[XmlAttribute(AttributeName = "xmlns")] | |
public string Xmlns { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment