I have an xml file and I want to the write the information on to the csv file.
I am trying to parse the xml to get the fields and the data and write onto a csv file. So far I have been able to parse the data to get the information in relation to
'title' and 'id' under node 'entry' but I am trying to parse the data under node '', 'id' information.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://google.com/en-US/syndicate/" xmlns:d="http://schemas.google.com/ado/2007/08/dataservices" xmlns:m="http://schemas.giooglt.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Partners</title>
<id>http://googlre.com/en-US/syndicate/Partners</id>
<updated>2014-01-16T21:33:20Z</updated>
< link rel="self" title="Partners" href="Partners" />
<entry>
<id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555')</id>
<title type="text">M55p; Co</title>
<summary type="text">cccc is a Certified Partner, reseller, and implementer of
Key industries we work with include:
• Financial services
• Professional services
• Media / publishing
By focusing on mid-market to enterprise clients,
</summary>
<published>2009-07-21T14:23:50-07:00</published>
<updated>2013-11-22T15:00:46-08:00</updated>
<author>
<name>google chrome</name>
<uri>http://google.com/</uri>
<email>retee#gmail.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('4255')" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links">
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/('429')/Links</id>
<updated>2014-01-16T21:33:20Z</updated>
<link rel="self" title="Links" href="Partners('4ff')/Links" />
<entry>
<id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
<title type="text">
</title>
<updated>2014-01-16T21:33:20Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" scheme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>pgooglrpartnerpage</d:Type>
<d:Description>google Partner Page</d:Description>
<d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet</d:Url>
</m:properties>
</content>
</entry>
</m:inline>
</entry>
<entry>
<id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555')</id>
<title type="text">M55p; Co</title>
<summary type="text">cccc is a Certified Partner, reseller, and implementer of
Key industries we work with include:
• Financial services
• Professional services
• Media / publishing
By focusing on mid-market to enterprise clients,
</summary>
<published>2009-07-21T14:23:50-07:00</published>
<updated>2013-11-22T15:00:46-08:00</updated>
<author>
<name>google chrome</name>
<uri>http://google.com/</uri>
<email>retee#gmail.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('4255')" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links">
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/('429')/Links</id>
<updated>2014-01-16T21:33:20Z</updated>
<link rel="self" title="Links" href="Partners('4ff')/Links" />
<entry>
<id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
<title type="text">
</title>
<updated>2014-01-16T21:33:20Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" scheme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>pgooglrpartnerpage</d:Type>
<d:Description>google Partner Page</d:Description>
<d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet</d:Url>
</m:properties>
</content>
</entry>
</m:inline>
</entry>
</feed>
The entries will be different but as one sees, I want to extract the information and write it onto a CSV file as -
Title,Id,PinpointId
//de:entry/de:title,//de:entry/de:title,//de:entry/de:link/m:inline/de:feed/de:id
for each and every entry.
The piece of code being used to get the information is :
// Alternate Method for getting the Fields from the XML file
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("C:/Users/Administrator/Downloads/direct.xml");
XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
xmlnm.AddNamespace("de","http://www.w3.org/2005/Atom");
xmlnm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
xmlnm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
ParseXML(xmlDocument, xmlnm);
Debug.WriteLine("\n---XML parsed---");
}
public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
String path = "C:/Users/Administrator/Downloads/data.csv";
var w = new StreamWriter(path);
XmlNodeList nodes = xmlFile.SelectNodes("//de:entry", xmlnm);
foreach (XmlNode node in nodes)
{
string titl = node["title"].InnerText;
string ide = node["id"].InnerText;
//string cty = node["link/m:inline/de:feed/de:id"].InnerText;
Debug.WriteLine("Data :" + titl+"ID :"+ide);
}
}
The output of the program is -
Data :MIG & CoID :http://pinpoint.microsoft.com/en-US/syndicate/Partners('4295719419')
I am trying to parse the data like.
string cty = node["link/m:inline/de:feed/de:id"].InnerText;
But the error generated.
"Object reference not set to an instance of an object."
The xml has got multiple entries and in similar format. New to C# how to get the information and write it on to the CSV file.Please help me.
First, your xml is not well formed. It will need to be corrected. Second, I am unsure exactly what node you are attempting to return with the query "link/m:inline/de:feed/de:id"
With that said, your node["link/m:inline/de:feed/de:id"] isn't valid, so it's not getting set to the XmlNode object you are expecting. That's because an XmlNode item expects the name of a node, not an xpath query. Use SelectSingleNode if you want to pass an xpath query. Something like this:
foreach (XmlNode node in nodes)
{
string titl = node["title"].InnerText;
string ide = node["id"].InnerText;
string cty;
var ctyNode = node.SelectSingleNode("link/m:inline/de:feed/de:id");
if (ctyNode != null)
{
cty = ctyNode.InnerText
}
Debug.WriteLine("Data :" + titl+"ID :"+ide);
}
Related
I have a function that retrieves the .InnerText for an attribute in an XML node:
string getPropertyFromNode_string(XmlNode node, string propertyName)
{
try
{
string selectString = "./empty:content/m:properties/d:";
return node.SelectSingleNode(selectString + propertyName, Utils.nmREST).InnerText;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
throw exception;
}
}
nmREST is defined in the constructor of a static Utils class as follows:
public static XmlNamespaceManager nmREST = new XmlNamespaceManager(new NameTable());
static Utils()
{
nmREST.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
nmREST.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
nmREST.AddNamespace("empty", "http://www.w3.org/2005/Atom");
nmREST.AddNamespace("z", "#RowsetSchema");
}
I test the function on this XmlNode:
<entry m:etag=""81"" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<id>Web/Lists(guid'someguid')/Items(1213)</id>
<category term="SP.Data.LibItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'someguid')/Items(1213)" />
<title />
<updated>2019-04-16T06:16:50Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">1213</d:Id>
<d:FileLeafRef xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">myfile.xlsm</d:FileLeafRef>
<d:FeatureCount m:type="Edm.Double" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">33</d:FeatureCount>
<d:Status xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">Production Ready</d:Status>
<d:CheckoutUserId m:null="true" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" />
<d:EditorId m:type="Edm.Int32" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">25</d:EditorId>
<d:ID m:type="Edm.Int32" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">1213</d:ID>
</m:properties>
</content>
</entry>
using this function call:
getPropertyFromNode_string(thisNode,"ID")
and the value 1213 is successfully retrieved.
However, when I test it on the following XmlNode:
<entry m:etag=""24"" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<id>Web/Lists(guid'someguid')/Items(1422)</id>
<category term="SP.Data.LibItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'someguid')/Items(1422)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Station" type="application/atom+xml;type=entry" title="Station" href="Web/Lists(guid'someguid')/Items(1422)/Station">
<m:inline>
<entry>
<id>anotherguid</id>
<category term="SP.Data.DifferentLibItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2019-04-16T05:58:17Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:FacilityNumber xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">1068</d:FacilityNumber>
</m:properties>
</content>
</entry>
</m:inline>
</link>
<title />
<updated>2019-04-16T05:58:17Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:FileLeafRef xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">thatfilename.xlsm</d:FileLeafRef>
<d:Title m:null="true" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" />
</m:properties>
</content>
</entry>
using the function call:
getPropertyFromNode_string(thisNode,"FacilityNumber")
then the SelectSingleNode() call throws an exception with the message:
Object reference not set to an instance of an object.
I guess this means that the XPath expression isn't successfully locating the <d:FacilityNumber> element, so there's no object to get InnerText from. Why isn't the element being found? What's different about the second node's XML structure, and what XPath expression should I be using instead?
As per Laurent Lequenne's comment on the OP, including the full nesting sequence of child nodes in the XPath expression successfully retrieves the desired child node, as follows:
./empty:link/m:inline/empty:entry/empty:content/m:properties/d:
I am getting XML response for REST call which includes multiple subnodes for each entry. Basically any Lookup field or User field results in a subnode.
Can anybody help me to understand how to read these subnodes? As the element name is Id or Email which is repetitive. Sorry for the very long XML but this is a result of fetching only 2 items.
<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://ppespcollab.amat.com/sites/SSAS/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<id>f7f3e0d8-73aa-4bd9-a11c-c8d2c884895c</id>
<title />
<updated>2017-09-14T09:51:55Z</updated>
<entry m:etag=""5"">
<id>Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)</id>
<category term="SP.Data.RequestsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Name" type="application/atom+xml;type=entry" title="Name" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Name">
<m:inline>
<entry>
<id>d724a6a3-c532-4f43-baaf-f3a25bbaccca</id>
<category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">31210</d:Id>
<d:EMail>Ishan_Halarnkar#contractor.amat.com</d:EMail>
</m:properties>
</content>
</entry>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Segment" type="application/atom+xml;type=entry" title="Segment" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Segment">
<m:inline>
<entry>
<id>9d4306ec-4b01-4624-984b-79445b9d2ec1</id>
<category term="SP.Data.SegmentsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>Test - TechM</d:Title>
<d:Id m:type="Edm.Int32">10</d:Id>
</m:properties>
</content>
</entry>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/BU" type="application/atom+xml;type=feed" title="BU" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/BU">
<m:inline>
<feed>
<id>7d3cf6e4-9fdc-4c21-90e9-5112b963dc49</id>
<title />
<updated>2017-09-14T09:51:55Z</updated>
<entry>
<id>44c3ff84-4650-4d85-a6dc-908639338be4</id>
<category term="SP.Data.Business_x0020_UnitsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>CMP</d:Title>
<d:Id m:type="Edm.Int32">14</d:Id>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Author0" type="application/atom+xml;type=feed" title="Author0" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Author0">
<m:inline>
<feed>
<id>0f399745-10f4-40f0-be4e-1f9d69222d49</id>
<title />
<updated>2017-09-14T09:51:55Z</updated>
<entry>
<id>4c0509ce-62c4-4e90-9689-134b145b02a0</id>
<category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">31210</d:Id>
<d:EMail>Ishan_Halarnkar#contractor.amat.com</d:EMail>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CC_x0020_List" type="application/atom+xml;type=feed" title="CC_x0020_List" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/CC_x0020_List" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Publication_x0020_Type" type="application/atom+xml;type=entry" title="Publication_x0020_Type" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Publication_x0020_Type">
<m:inline>
<entry>
<id>a002d855-e51b-4f4f-b8ec-3725cf4aecb9</id>
<category term="SP.Data.Publication_x0020_TypeListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>Presentation</d:Title>
<d:Id m:type="Edm.Int32">2</d:Id>
</m:properties>
</content>
</entry>
</m:inline>
</link>
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">160</d:Id>
<d:Title>asssfsfdsf</d:Title>
<d:End_x0020_Use>BLR</d:End_x0020_Use>
<d:Approval_x0020_Type>Parallel</d:Approval_x0020_Type>
<d:Due_x0020_Date m:type="Edm.DateTime">2017-09-26T05:00:00Z</d:Due_x0020_Date>
<d:Description>sdfssfsfssf</d:Description>
<d:Status>Submitted</d:Status>
<d:History>Request submitted by Ishan Halarnkar --CNTR on Wednesday, September 13, 2017 8:15 AM</br></d:History>
<d:RequestID>2017_00000160</d:RequestID>
<d:EndUseYear>2014</d:EndUseYear>
<d:ID m:type="Edm.Int32">160</d:ID>
</m:properties>
</content>
</entry>
<entry m:etag=""7"">
<id>Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)</id>
<category term="SP.Data.RequestsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Name" type="application/atom+xml;type=entry" title="Name" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Name">
<m:inline>
<entry>
<id>b76cc22b-f985-444a-873a-46ae2cd0a10a</id>
<category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">6760</d:Id>
<d:EMail>Denise_Schmidt#amat.com</d:EMail>
</m:properties>
</content>
</entry>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Segment" type="application/atom+xml;type=entry" title="Segment" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Segment">
<m:inline>
<entry>
<id>6e70b80b-fff6-4662-8f53-26fc977f34ef</id>
<category term="SP.Data.SegmentsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>Testing Segment 0917</d:Title>
<d:Id m:type="Edm.Int32">11</d:Id>
</m:properties>
</content>
</entry>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/BU" type="application/atom+xml;type=feed" title="BU" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/BU">
<m:inline>
<feed>
<id>f5e5e76d-07b7-442f-b6bf-73d9ed65e708</id>
<title />
<updated>2017-09-14T09:51:55Z</updated>
<entry>
<id>07bd9eed-23f7-4b10-8f38-42e0845f19ca</id>
<category term="SP.Data.Business_x0020_UnitsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>ds Testing</d:Title>
<d:Id m:type="Edm.Int32">24</d:Id>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Author0" type="application/atom+xml;type=feed" title="Author0" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Author0">
<m:inline>
<feed>
<id>717cf9a2-9f0a-4265-bb32-1c90989ba6ed</id>
<title />
<updated>2017-09-14T09:51:55Z</updated>
<entry>
<id>4c593a4e-5c2e-4f1a-8c99-cb2d81b33af3</id>
<category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">6760</d:Id>
<d:EMail>Denise_Schmidt#amat.com</d:EMail>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CC_x0020_List" type="application/atom+xml;type=feed" title="CC_x0020_List" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/CC_x0020_List" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Publication_x0020_Type" type="application/atom+xml;type=entry" title="Publication_x0020_Type" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Publication_x0020_Type">
<m:inline>
<entry>
<id>62f94289-1e4b-4e83-94fe-1f518bde639b</id>
<category term="SP.Data.Publication_x0020_TypeListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>Presentation</d:Title>
<d:Id m:type="Edm.Int32">2</d:Id>
</m:properties>
</content>
</entry>
</m:inline>
</link>
<title />
<updated>2017-09-14T09:51:55Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">159</d:Id>
<d:Title>Test on 9/12/17</d:Title>
<d:End_x0020_Use>TPAS</d:End_x0020_Use>
<d:Approval_x0020_Type>Parallel</d:Approval_x0020_Type>
<d:Due_x0020_Date m:type="Edm.DateTime">2017-09-28T05:00:00Z</d:Due_x0020_Date>
<d:Description>testing email notices</d:Description>
<d:Status>Submitted</d:Status>
<d:History>Request delegated by Denise Schmidt (ds Testing - CTO) on Tuesday, September 12, 2017 2:16 PM</br>Request approved by Denise Schmidt (ds Testing - Manager) on Tuesday, September 12, 2017 2:15 PM</br>Request submitted by Denise Schmidt on Tuesday, September 12, 2017 2:12 PM</br></d:History>
<d:RequestID>2017_00000159</d:RequestID>
<d:EndUseYear m:null="true" />
<d:ID m:type="Edm.Int32">159</d:ID>
</m:properties>
</content>
</entry>
The code i am using is but is not giving the expected results :
XmlDocument listXml = new XmlDocument();
XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());
xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
listXml.LoadXml(response);
var prop = listXml.SelectNodes("//atom:entry/atom:content/m:properties", xmlnspm);
List<string> sdsd = new List<string>();
foreach (XmlNode ndlist in prop)
{
sdsd.Add(ndlist.SelectSingleNode("d:Id", xmlnspm).InnerXml +
ndlist.SelectSingleNode("d:EMail", xmlnspm).InnerXml +
ndlist.SelectSingleNode("d:Status", xmlnspm).InnerXml+
ndlist.SelectSingleNode("d:History", xmlnspm).InnerXml);
}
The result i am looking for is :
For each entry, read the
If Name then Name(Id and Email)
If Segment then Segment(Title and Id)
If BU then BU(Title and Id)
If Author0 then Author0(Id and Email)
...likewise for all subnodes
and for all main nodes i need the value
Id
Title
End_x0020_Use
Approval_x0020_Type
Is there any way to Serialize this data in a more readable format or i'l have to loop each and every node?
Please suggest the best option as i don't have much experience in XML data.
Use xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication4
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static List<XElement> nodes;
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement feed = doc.Root;
XNamespace ns = feed.GetDefaultNamespace();
foreach (XElement entry in feed.Elements(ns + "entry"))
{
Entry newEntry = new Entry();
Entry.entries.Add(newEntry);
newEntry.title = (string)entry.Element(ns + "title");
newEntry.end_x0020_Use = (string)entry.Descendants().Where(x => x.Name.LocalName == "End_x0020_Use").FirstOrDefault();
newEntry.approval_x0020_Type = (string)entry.Descendants().Where(x => x.Name.LocalName == "Approval_x0020_Type").FirstOrDefault();
newEntry.links = entry.Elements(ns + "link").Select(x => x.Descendants().Where(y => y.Name.LocalName == "entry").Select(y => new Link() {
name = (string)y.Descendants().Where(z => z.Name.LocalName == "name").FirstOrDefault(),
id = (string)y.Descendants().Where(z => z.Name.LocalName == "id").FirstOrDefault(),
email = (string)y.Descendants().Where(z => z.Name.LocalName == "EMail").FirstOrDefault(),
title = (string)x.Attributes().Where(z => z.Name.LocalName == "title").FirstOrDefault()
}).FirstOrDefault()
).ToList();
}
}
}
public class Entry
{
public static List<Entry> entries = new List<Entry>();
public string title { get; set;}
public string end_x0020_Use { get; set; }
public string approval_x0020_Type { get; set; }
public List<Link> links { get; set; }
}
public class Link
{
public string name { get; set; }
public string id { get; set; }
public string title { get; set; }
public string email { get; set; }
}
}
I would advise you to use $filter in your REST API call so that you would get a JSON result which is pretty much easy to work with. The XML retrieval may fail sometimes.
The Xml for the file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://google.com/en-US/syndicate/" xmlns:d="http://schemas.google.com/ado/2007/08/dataservices" xmlns:m="http://schemas.giooglt.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Partners</title>
<id>http://googlre.com/en-US/syndicate/Partners</id>
<updated>2014-01-16T21:33:20Z</updated>
<link rel="self" title="Partners" href="Partners" />
<entry>
<id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555')</id>
<title type="text">M55p; Co</title>
<summary type="text">
cccc is a Certified Partner, reseller, and implementer of
Key industries we work with include:
• Financial services
• Professional services
• Media / publishing
By focusing on mid-market to enterprise clients,
</summary>
<published>2009-07-21T14:23:50-07:00</published>
<updated>2013-11-22T15:00:46-08:00</updated>
<author>
<name>google chrome</name>
<uri>http://google.com/</uri>
<email>retee#gmail.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('4255')" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links">
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/('429')/Links</id>
<updated>2014-01-16T21:33:20Z</updated>
<link rel="self" title="Links" href="Partners('4ff')/Links" />
<entry>
<id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
<title type="text">
</title>
<updated>2014-01-16T21:33:20Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" sch="" eme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>pgooglrpartnerpage</d:Type>
<d:Description>google Partner Page</d:Description>
<d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://googlet.com/en-US/syndicate/Links('tpartnerrfipage')</id>
<title type="text">
</title>
<updated>2014-01-19T04:01:49Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('pinpointpartnerrfipage')" />
<category term="google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.google.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>tpartnerrfipage</d:Type>
<d:Description>RFI Page</d:Description>
<d:Url>http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&wt.mc_id=54545</d:Url>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
</entry>
<entry>
<id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('45')</id>
<title type="text">vfere</title>
<summary type="text">
cccc is a Certified Partner, reseller, and implementer of
Key industries we work with include:
• Financial services
• Professional services
• Media / publishing
By focusing on mid-market to enterprise clients,
</summary>
<published>2009-07-21T14:23:50-07:00</published>
<updated>2013-11-22T15:00:46-08:00</updated>
<author>
<name>google chrome</name>
<uri>http://google.com/</uri>
<email>retee#gmail.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('4255')" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links" >
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/('429')/Links</id>
<updated>2014-01-16T21:33:20Z</updated>
<link rel="self" title="Links" href="Partners('4ff')/Links" />
<entry>
<id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
<title type="text">
</title>
<updated>2014-01-16T21:33:20Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" scheme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>pgooglrpartnerpage</d:Type>
<d:Description>google Partner Page</d:Description>
<d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://googlet.com/en-US/syndicate/Links('tpartnerrfipage')</id>
<title type="text">
</title>
<updated>2014-01-19T04:01:49Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('pinpointpartnerrfipage')" />
<category term="google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.google.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>tpartnerrfipage</d:Type>
<d:Description>RFI Page</d:Description>
<d:Url>http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&wt.m</d:Url>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
</entry>
</feed>
I have a piece of code where I want to simply console output the values of each Urls.
var reader = new StreamReader(#"C:/Users/Administrator/Downloads/direct.xml")
var xmlDoc = XDocument.Load(reader);
XNamespace atom = "http://www.w3.org/2005/Atom";
XNamespace metadata = "http://schemas.giooglt.com/ado/2007/08/dataservices/metadata";
XNamespace dataservices = "http://schemas.google.com/ado/2007/08/dataservices";
var result = xmlDoc.Root.Elements(atom + "entry")
.Select(e => new {
Title = e.Element(atom + "title").Value,
Id = e.Element(atom + "id").Value,
Urls = e.Elements(atom + "link")
.Where(l => l.Element(metadata + "inline") != null)
.SelectMany(l => l.Element(metadata + "inline")
.Element(atom + "feed")
.Elements(atom + "entry")
.Select(e1 => e1.Element(atom + "content")
.Element(metadata + "properties")
.Element(dataservices + "Url").Elements.Select(url => url.Value)))
});
foreach (var item in result)
{
Debug.WriteLine("{0}, {1}", item.Title, item.Id);
var urls = System.String.Join(",",item.Urls);
Debug.WriteLine(urls);
}
Here Urls are of type 'Ienumerable Urls'. How to get the values of every element in Urls.
desired output -
M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),http://googlgt.com/en-US/PartnerDetails.aspx? PartnerId=42555&wt.mc_id=66ttet
M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&wt.mc_id=54545
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http: //pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&wt.m
Any help is appreciated.
Actual Output
M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet,System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&wt.mc_id=54545,System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet, System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http: //pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&wt.mSystem.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
Ah, I see. You want a comma-separated list of the URLs. As you pointed out, your Urls property is an IEnumerable of strings, so you need to actually iterate over those strings to build the final string you're looking for.
Try this:
var result = xmlDoc.Root.Elements(atom + "entry")
.Select(e => new {
Title = e.Element(atom + "title").Value,
Id = e.Element(atom + "id").Value,
Urls = e.Elements(atom + "link")
.Where(l => l.Element(metadata + "inline") != null)
.SelectMany(l => l.Element(metadata + "inline")
.Element(atom + "feed")
.Elements(atom + "entry")
.Select(e1 => e1.Element(atom + "content")
.Element(metadata + "properties")
.Element(dataservices + "Url").Value))
});
foreach(var r in result)
{
Debug.WriteLine("{0},{1},{2}", r.Title, r.Id, String.Join(",", r.Urls));
}
Just curious: since you're apparently parsing an ATOM feed, have you looked into packages that will do this for you? https://www.nuget.org/packages?q=atom
I've a problem to parse my XML File (RSS Feed) in C#.
I just want to read out the "entry" entries (the root parent - "feed" - is not relevant).
All "entry" entries are almost even, except the part "state". Some entries doesn't have that entry.
So i just want to read out the following:
"entry" nodes:
updated
expires
title
summary
state (if exists)
Any suggestions?
Thank you very much.
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<updated>2011-01-01T00:00:00+0100</updated>
<link href="http://www.domain.com" rel="self"/>
<author>
<name>Mr X</name>
<email>Mr_X#domain.com</email>
</author>
<title>Some infos....</title>
<id>domain.com</id>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My first Title</title>
<id>First ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/firstElement"></link>
<summary>My first important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/firstElement.png"/>
</div>
</div>
</content>
</entry>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My second Title</title>
<state>active</state>
<id>Second ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/secondElement"></link>
<summary>My second important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/secondElement.png"/>
</div>
</div>
</content>
</entry>
</feed>{<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<updated>2011-01-01T00:00:00+0100</updated>
<link href="http://www.domain.com" rel="self"/>
<author>
<name>Mr X</name>
<email>Mr_X#domain.com</email>
</author>
<title>Some infos....</title>
<id>domain.com</id>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My first Title</title>
<id>First ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/firstElement"></link>
<summary>My first important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/firstElement.png"/>
</div>
</div>
</content>
</entry>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My second Title</title>
<state>active</state>
<id>Second ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/secondElement"></link>
<summary>My second important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/secondElement.png"/>
</div>
</div>
</content>
</entry>
</feed>
My current C# code:
public void ParseXML(XmlDocument xmlFile)
{
ArrayList updated = new ArrayList();
ArrayList expires = new ArrayList();
ArrayList title = new ArrayList();
ArrayList summary = new ArrayList();
ArrayList state = new ArrayList();
ObservableCollection<TrafficInformation> trafInfo = new ObservableCollection<TrafficInformation>();
myCollection = trafInfo;
XmlNodeReader reader = new XmlNodeReader(xmlFile);
StringBuilder output = new StringBuilder();
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if(reader.Name == "updated")
{
updated.Add(reader.ReadString());
}
if (reader.Name == "expires")
{
expires.Add(reader.ReadString());
}
if (reader.Name == "title")
{
title.Add(reader.ReadString());
}
if (reader.Name == "summary")
{
summary.Add(reader.ReadString());
}
if (reader.Name == "state")
{
state.Add(reader.ReadString());
}
break;
}
}
}
In that case, I don't have a relationship between the data (if state doesn't exists).
I believe the easiest way to parse the XML directly is using LINQ-TO-XML. You can find more info here.
You could use XPath expression for that. Below is complete example on console-appliactaion - as you use xlmns namespace, it requries a little modification of ParseXML method.
using System;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("XMLFile1.xml");
XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
xmlnm.AddNamespace("ns", "http://www.w3.org/2005/Atom");
ParseXML(xmlDocument, xmlnm);
Console.WriteLine("\n---XML parsed---");
Console.ReadKey();
}
public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
XmlNodeList nodes = xmlFile.SelectNodes("//ns:updated | //ns:expires | //ns:title | //ns:summary | //ns:state", xmlnm);
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Name + " = " + node.InnerXml);
}
}
}
}
// in XPath expression means, you want to select all nodes with specific name, no matter where they are.
If you want to search only <entry></entry> elements, you can use following:
"//ns:entry/ns:updated | //ns:entry/ns:expires | //ns:entry/ns:title | //ns:entry/ns:summary | //ns:entry/ns:state"
i am trying to parse an xml element using XMLDocument (DItem >> Title)
below is my code but somehow i am not getting hold of it.... any help?
XmlDocument xmldoc = new XmlDocument();
XmlNamespaceManager xmlns = new XmlNamespaceManager(xdoc.NameTable);
xmlns.AddNamespace("DItems", "http://namespace.xsd");
xmldoc.Load(url);
var title = xmldoc.SelectNodes("content", xmlns);
foreach (XmlNode node in title)
{
string title = node.Attributes["Title"].Value;
//this.ddlTitle.Items.Add(new ListItem(title));
}
here is my XML:
<?xml version='1.0'?>
<root xmlns="http://www.w3.org/2005/Atom">
<title type="text">title</title>
<entry>
<content type="application/xml">
<Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.namespace.xsd">
<CatalogSource Acronym="ABC" OrganizationName="organization name" />
<Item Id="28466" CatalogUrl="url">
<DItem xmlns:content="http://namespace.xsd" TargetUrl="http://index.html" Title="my title1">
<content:Source Acronym="ABC" OrganizationName="ABC" />
</DItem>
</Item>
</Items>
</content>
</entry>
<entry>
<content type="application/xml">
<Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.namespace.xsd">
<CatalogSource Acronym="ABC" OrganizationName="organization name" />
<Item Id="28466" CatalogUrl="url">
<DItem xmlns:content="http://namespace.xsd" TargetUrl="http://index.html" Title="my title2">
<content:Source Acronym="ABC" OrganizationName="ABC" />
</DItem>
</Item>
</Items>
</content>
</entry>
<entry>
<content type="application/xml">
<Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.namespace.xsd">
<CatalogSource Acronym="ABC" OrganizationName="organization name" />
<Item Id="28466" CatalogUrl="url">
<DItem xmlns:content="http://namespace.xsd" TargetUrl="http://index.html" Title="my title3">
<content:Source Acronym="ABC" OrganizationName="ABC" />
</DItem>
</Item>
</Items>
</content>
</entry>
</root>
var xmldoc = new XmlDocument();
var xmlns = new XmlNamespaceManager(xmldoc.NameTable);
xmlns.AddNamespace("DItems", "http://www.namespace.xsd");
xmldoc.Load(url);
var titleNodes = xmldoc.SelectNodes("//DItems:DItem/#Title", xmlns);
var result = titleNodes.Cast<XmlAttribute>().Select(a => a.Value).ToList();
Output (list of objects):
my title1
my title2
my title3