How to Get the Id variable from the XML in c#? - c#

Full XML for reference as follows
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://pinpoint.microsoft.com/en-US/syndicate/"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://www.w3.org/2005/Atom">
<title type="text">Partners</title>
<id>http://google.com/en-US/syndicate/Partners</id>
<updated>2014-01-20T12:29:25Z</updated>
<link rel="self" title="Partners" href="Partners" />
<entry>
<id>http://google.com/en-US/syndicate/Partners('429')</id>
<title type="text">Light Systems</title>
<summary type="text">
How may we help you? will help you address concerns like security and productivity that may be delaying your transition to a work anywhere, anytime workforce. Mac, Unix and Linux platforms. Whatever your size or need, we have an IT plan that will work for your business.
</summary>
<published>2009-07-27T14:23:39-07:00</published>
<updated>2014-01-10T15:00:41-08:00</updated>
<author>
<name>Google Production</name>
<uri>http://google.com/</uri>
<email>calamity#google.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('429')" />
<link rel="http://google.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed"
title="Links" href="Partners('429')/Links">
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/en-US/syndicate/Partners('429')/Links</id>
<updated>2014-01-20T12:29:25Z</updated>
<link rel="self" title="Links" href="Partners('429')/Links" />
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerpage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerpage</d:Type>
<d:Description>Partner Page</d:Description>
<d:Url>http://google.com/en-US/PartnerDetails.aspx?PartnerId=429&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerproductlist')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerproductlist')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerproductlist</d:Type>
<d:Description>Partner Product List</d:Description>
<d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=429&CurrentTab=1&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerreviewpage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerreviewpage')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerreviewpage</d:Type>
<d:Description>Partner Review Page</d:Description>
<d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=429&CurrentTab=2&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerlocationpage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerlocationpage')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerlocationpage</d:Type>
<d:Description>Partner Location Page</d:Description>
<d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=429&CurrentTab=3&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerrfipage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerrfipage')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerrfipage</d:Type>
<d:Description>Partner RFI Page</d:Description>
<d:Url>http://google.microsoft.com/en-US/RFI.aspx?partnerId=429&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerwebsite')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerwebsite')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerwebsite</d:Type>
<d:Description>Website</d:Description>
<d:Url>http://google.com/en-US/syndicate/relay/partner/website?id=429&link=http%3a%2f%2fwww.systems.com&TrackID=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerlogo')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerlogo')" />
<category term="Microsoft.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerlogo</d:Type>
<d:Description>Logo</d:Description>
<d:Url>http://www.systems.com/lan_logo.jpg</d:Url>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://google.com/ado/2007/08/dataservices/related/Locations" type="application/atom+xml;type=feed"
title="Locations" href="Partners('429')/Locations">
<m:inline>
<feed>
<title type="text">Locations</title>
<id>http://google.com/en-US/syndicate/Partners('429')/Locations</id>
<updated>2014-01-20T12:29:25Z</updated>
<link rel="self" title="Locations" href="Partners('429')/Locations" />
<entry>
<id>http://google.com/en-US/syndicate/Locations(1)</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Location" href="Locations(1)" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Location"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm">1</d:Id>
<d:Address>Crossing Park, Suite E</d:Address>
<d:City>Norwalk</d:City>
<d:State>CT</d:State>
<d:Country>USA</d:Country>
<d:PostalCode>30071</d:PostalCode>
<d:HQ m:type="Edm.Boolean">true</d:HQ>
<d:Latitude m:type="Edm.Double">566</d:Latitude>
<d:Longitude m:type="Edm.Double">-56.56</d:Longitude>
<d:ContactName m:null="true" />
<d:ContactPhoneNumber>+1 9175391100</d:ContactPhoneNumber>
<d:ContactPhoneExtension m:null="true" />
<d:ContactFaxNumber>+1 9175391100</d:ContactFaxNumber>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://google.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed"
title="Products" href="Partners('429')/Products" />
<link rel="http://google.com/ado/2007/08/dataservices/related/Categories" type="application/atom+xml;type=feed"
title="Categories" href="Partners('429')/Categories" />
<category term="google.Marketplace.Syndicate.V2010_05.Partner"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:View>v244</d:View>
<d:Id>429</d:Id>
<d:NativeId>630</d:NativeId>
<d:Name>Systems</d:Name>
<d:Description xml:space="preserve">will help </d:Description>
<d:RatingAverage m:type="Edm.Decimal">555</d:RatingAverage>
<d:RatingCount m:type="Edm.Int32">56</d:RatingCount>
<d:Certification>Registered Member</d:Certification>
<d:Search m:null="true" />
<d:Loc m:null="true" />
<d:Dist m:null="true" />
<d:Cat m:null="true" />
</m:properties>
</content>
<rights type="text">Copyright &copy; 2011 Google Corporation</rights>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Partners('4294')</id>
<title type="text">Wholesale Intl.</title>
<summary type="text">SQL Server </summary>
<published>2009-07-21T14:23:50-07:00</published>
<updated>2013-08-20T02:17:21-07:00</updated>
<author>
<name>google</name>
<uri>http://google.com/</uri>
<email>gmail.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('4294')" />
<link rel="http://google.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed"
title="Links" href="Partners('4294')/Links">
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/en-US/syndicate/Partners('4294')/Links</id>
<updated>2014-01-20T12:29:25Z</updated>
<link rel="self" title="Links" href="Partners('4294')/Links" />
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerpage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerpage</d:Type>
<d:Description>Page</d:Description>
<d:Url>http://google.microsoft.com/en-US/PartnerDetails.aspx?PartnerId=4294&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerproductlist')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerproductlist')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerproductlist</d:Type>
<d:Description>Product List</d:Description>
<d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=4294&CurrentTab=1&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerreviewpage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('pinpointpartnerreviewpage')" />
<category term="Google.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerreviewpage</d:Type>
<d:Description>Review Page</d:Description>
<d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=4294&CurrentTab=2&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerlocationpage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerlocationpage')" />
<category term="google.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerlocationpage</d:Type>
<d:Description>Location Page</d:Description>
<d:Url>http://google.com/en-US/PartnerDetails.aspx?partnerId=4294&CurrentTab=3&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerrfipage')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerrfipage')" />
<category term="Google.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerrfipage</d:Type>
<d:Description>RFI Page</d:Description>
<d:Url>http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4294&wt.mc_id=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerwebsite')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerwebsite')" />
<category term="google.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerwebsite</d:Type>
<d:Description>Website</d:Description>
<d:Url>http://google.com/en-US/syndicate/relay/partner/website?id=4294&link=http%3a%2f%2fwww.software-intl.com&TrackID=ht43</d:Url>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Links('partnerlogo')</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerlogo')" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Link"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>partnerlogo</d:Type>
<d:Description>Logo</d:Description>
<d:Url>http://www.software-intl.com/uploads/2012/04/swi-logo-web.png</d:Url>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://google.com/ado/2007/08/dataservices/related/Locations" type="application/atom+xml;type=feed"
title="Locations" href="Partners('4294')/Locations">
<m:inline>
<feed>
<title type="text">Locations</title>
<id>http://google.com/en-US/syndicate/Partners('4294')/Locations</id>
<updated>2014-01-20T12:29:25Z</updated>
<link rel="self" title="Locations" href="Partners('4294')/Locations" />
<entry>
<id>http://google.com/en-US/syndicate/Locations(1)</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Location" href="Locations(1)" />
<category term="Google.Marketplace.Syndicate.V2010_05.Location"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm">1</d:Id>
<d:Address>1114</d:Address>
<d:City>Longt</d:City>
<d:State>CT</d:State>
<d:Country>USA</d:Country>
<d:PostalCode>80455</d:PostalCode>
<d:HQ m:type="Edm.Boolean">true</d:HQ>
<d:Latitude m:type="Edm.Double">4455</d:Latitude>
<d:Longitude m:type="Edm.Double">444</d:Longitude>
<d:ContactName>xxxx</d:ContactName>
<d:ContactPhoneNumber>+1 917000000</d:ContactPhoneNumber>
<d:ContactPhoneExtension m:null="true" />
<d:ContactFaxNumber>+1 917000000</d:ContactFaxNumber>
</m:properties>
</content>
</entry>
<entry>
<id>http://google.com/en-US/syndicate/Locations(2)</id>
<title type="text">
</title>
<updated>2014-01-20T12:29:25Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Location" href="Locations(2)" />
<category term="google.Marketplace.Syndicate.V2010_05.Location"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm">2</d:Id>
<d:Address>Suite 206</d:Address>
<d:City>Moo</d:City>
<d:State>CT</d:State>
<d:Country>USA</d:Country>
<d:PostalCode>88154</d:PostalCode>
<d:HQ m:type="Edm.Boolean">false</d:HQ>
<d:Latitude m:type="Edm.Double">32.01</d:Latitude>
<d:Longitude m:type="Edm.Double">-333</d:Longitude>
<d:ContactName>cddc</d:ContactName>
<d:ContactPhoneNumber>1-917000000</d:ContactPhoneNumber>
<d:ContactPhoneExtension>457</d:ContactPhoneExtension>
<d:ContactFaxNumber>917000000</d:ContactFaxNumber>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products"
type="application/atom+xml;type=feed" title="Products" href="Partners('4294')/Products" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories"
type="application/atom+xml;type=feed" title="Categories" href="Partners('4294')/Categories" />
<category term="Google.Commerce.Marketplace.Syndicate.V2010_05.Partner"
scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:View>v45</d:View>
<d:Id>4294</d:Id>
<d:NativeId>7104</d:NativeId>
<d:Name>Wholesale Intl.</d:Name>
<d:Description> license or a comprehensive company-wide license agreement we can assist you to license your Microsoft software. </d:Description>
<d:RatingAverage m:type="Edm.Decimal">455</d:RatingAverage>
<d:RatingCount m:type="Edm.Int32">55</d:RatingCount>
<d:Certification>hagh</d:Certification>
<d:Search m:null="true" />
<d:Loc m:null="true" />
<d:Dist m:null="true" />
<d:Cat m:null="true" />
</m:properties>
</content>
<rights type="text">Copyright &copy; 2011 Google Corporation</rights>
</entry>
</feed>
The code being used to generate the information is
using( var w1 = new StreamWriter(path1,true))
{
// Read and parse the required information from 'direct.xml'
var reader1 = new StreamReader("C:/Users/Administrator/Downloads/direct.xml");
var xmlDoc1 = XDocument.Load(reader1);
XNamespace atom = "http://www.w3.org/2005/Atom";
XNamespace metadata = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace dataservices = "http://schemas.microsoft.com/ado/2007/08/dataservices";
var result = xmlDoc1.Root.Elements(atom + "entry")
.Select(e => new
{
Title = (string)e.Element(atom + "title"),
Id = (string)e.Element(atom + "id"),
// 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 => (string)e1.Element(atom + "content")
// .Element(metadata + "properties")
//.Element(dataservices + "Url")).Where(k => k != null)),
City = 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 => (string)e1.Element(atom + "content")
.Element(metadata + "properties")
.Element(dataservices + "City"))
.Where(u => u != null)),
State = 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 => (string)e1.Element(atom + "content")
.Element(metadata + "properties")
.Element(dataservices + "State"))
.Where(u => u != null)),
Country = 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 => (string)e1.Element(atom + "content")
.Element(metadata + "properties")
.Element(dataservices + "Country"))
.Where(u => u != null)),
Identity = e.Element(atom +"content")
.Element(metadata + "properties")
.Element(dataservices + "Id")
});
foreach (var entry in result)
{
foreach (var sta in entry.State)
{
foreach (var ci in entry.City)
{
foreach (var coun in entry.Country)
{
// Debug.WriteLine("{0},{1},{2},{3}", entry.Title, entry.Id, ci);
String uniqueId = BusinessNeedNumber + IndustryFocusNumber + Competency + sta + ci + coun+entry.Identity;
Debug.WriteLine(uniqueId);
// just getting the Id value by replacement
// Writing onto CSV file
String advertisername = entry.Title.Replace(",", "&");
var data = string.Format(advertisername + "," + BusinessNeedNumber + "," + IndustryFocusNumber
+ "," + Competency + "," + URL + "," + signature + "," + SignatureURL + "," + sta + "," + ci + "," + coun + "," + uniqueId + "," + entry.Identity );
w1.WriteLine(data, true);
w1.Flush();
}
}
}
}
w1.Close();
reader1.Close();
return;
I want to parse the id field to receive the Id information which is under
entry,content,properties,id
Desired output is
4294
which is here
<d:Id>4294</d:Id>
But I am getting the output as
<d:Id xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">4294</d:Id>
Do not understand why am I getting that particular information and not just the 'Id' value
Thanks for help in advance.

Use entry.Identity.Value instead of entry.Identity:
String uniqueId = BusinessNeedNumber + IndustryFocusNumber + Competency + sta + ci + coun+entry.Identity.Value;

Related

LINQ remove method leaves a empty tag in xml [duplicate]

This question already has answers here:
Force XmlDocument to save empty elements with an explicit closing tag
(1 answer)
Forcing XDocument.ToString() to include the closing tag when there is no data
(2 answers)
Closed 11 months ago.
I am looking for a good approach to write a LINQ query that can preserve empty tags in XML.
Input XElement:
<LayoutDefinition >
<ScreenDefinitions>
<ScreenDefinition Monitor="1">
<SSBS>
<SSB Id="1" />
<SSB Id="2" />
</SSBS>
</ScreenDefinition>
<ScreenDefinition Monitor="2">
<SSBS>
<SSB Id="1" />
<SSB Id="2" />
</SSBS>
</ScreenDefinition>
</ScreenDefinitions>
<CustomProperties>
<Property Name="SynchronizationCollection" />
</CustomProperties>
</LayoutDefinition>
This is being process by the below method:
private static IList<XElement> ProcessLayoutDefinition(XElement layoutDefinition)
{
IList<XElement> resultLayoutDefinitions = new List<XElement>();
layoutDefinition?.XPathSelectElement($"/CustomProperties/Property[#Name=\"SynchronizationCollection\"]").Remove();
resultLayoutDefinitions.Add(layoutDefinition);
return resultLayoutDefinitions;
}
The Result should yield the below output:
<LayoutDefinition >
<ScreenDefinitions>
<ScreenDefinition Monitor="1">
<SSBS>
<SSB Id="1" />
<SSB Id="2" />
</SSBS>
</ScreenDefinition>
<ScreenDefinition Monitor="2">
<SSBS>
<SSB Id="1" />
<SSB Id="2" />
</SSBS>
</ScreenDefinition>
</ScreenDefinitions>
<CustomProperties>
</CustomProperties>
</LayoutDefinition>
But gives the below output with only <CustomProperties />
<LayoutDefinition >
<ScreenDefinitions>
<ScreenDefinition Monitor="1">
<SSBS>
<SSB Id="1" />
<SSB Id="2" />
</SSBS>
</ScreenDefinition>
<ScreenDefinition Monitor="2">
<SSBS>
<SSB Id="1" />
<SSB Id="2" />
</SSBS>
</ScreenDefinition>
</ScreenDefinitions>
<CustomProperties />
</LayoutDefinition>
Is there anyway I can force to preserve the <CustomProperties> </CustomProperties> tags although <CustomProperties /> is a correct tag.

How to write xpath for xml with two namespaces?

I am trying to get the values of the title and link with the attribute equals to alternate. But with the namespaces, I find it a bit challenging to get the values.
I have added my namespaces as follows but my result is comming back with Enumeration yeilds no result:
nameSpaceManager_ = new XmlNamespaceManager(new NameTable());
nameSpaceManager_.AddNamespace("viz", "http://www.vizrt.com/atom");
nameSpaceManager_.AddNamespace("atom", "http://www.w3.org/2005/Atom");
I am using XDocument with a mixture of linq and xpath to query my data.
I use the XPath as follows:
var showName = showNode.XPathEvaluate("/atom:entry/atom:title/text()", nameSpaceManager_);
UPDATE
XML:
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:base="http://127.0.0.1:8580/directory/shows/" xmlns="http://www.w3.org/2005/Atom">
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:viz="http://www.vizrt.com/atom">
<title>My Show</title>
<author>
<name>Media Sequencer</name>
</author>
<id>tag:user,2017-02-03:0:/directory/shows/My%20Show.show</id>
<updated>2017-02-03T11:41:05Z</updated>
<summary>Show My Show</summary>
<category scheme="http://www.vizrt.com/types" term="directory" />
<category scheme="http://www.vizrt.com/types" term="show" />
<category scheme="http://www.vizrt.com/types" term="trio_4_layer_collection" label="Trio 4 Layer Collection" />
<link type="application/atom+xml;type=feed" rel="alternate" href="http://127.0.0.1:8580/show/%7B4575C71F-FC79-4813-A92F-D6297D5C517C%7D/" />
<link type="application/atom+xml;type=entry" rel="self" href="http://127.0.0.1:8580/directory/shows/My%20Show.show" />
<viz:empty>false</viz:empty>
</entry>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:viz="http://www.vizrt.com/atom">
<title>My Show 2</title>
<author>
<name>Media Sequencer</name>
</author>
<id>tag:user,2017-02-03:0:/directory/shows/My%20Show.show</id>
<updated>2017-02-03T11:41:05Z</updated>
<summary>Show My Show</summary>
<category scheme="http://www.vizrt.com/types" term="directory" />
<category scheme="http://www.vizrt.com/types" term="show" />
<category scheme="http://www.vizrt.com/types" term="trio_4_layer_collection" label="Trio 4 Layer Collection" />
<link type="application/atom+xml;type=feed" rel="alternate" href="http://127.0.0.1:8580/show/%7B4575C71F-FC79-4813-A92F-D6297D5C517C%7D/" />
<link type="application/atom+xml;type=entry" rel="self" href="http://127.0.0.1:8580/directory/shows/My%20Show.show" />
<viz:empty>false</viz:empty>
</entry>
</feed>
Updated Query:
var exEl = xmlDoc.XPathSelectElements("//atom:feed/atom:entry[atom:category/#term='show']", nameSpaceManager_);
foreach (var showNode in exEl.Cast<XElement>())
{
var showName = showNode.XPathSelectElement("/atom:entry/atom:title", nameSpaceManager_).Value;
var linkTypes = showNode.XPathSelectElements("/atom:entry/atom:link[#rel='alternate']", nameSpaceManager_)
.Select(e => e.Attribute("type").Value);
}
You do almost everything right.
Just use XPathSelectElement instead of XPathEvaluate.
var showName = showNode.XPathSelectElement("/atom:entry/atom:title", nameSpaceManager_).Value;
var linkTypes = showNode.XPathSelectElements("/atom:entry/atom:link[#rel='alternate']", nameSpaceManager_)
.Select(e => e.Attribute("type").Value);

How to get the multiple values from xml?

My xml format is like
<?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 written the code with some help so as to parse the Xml and get the data as
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").Value))
});
foreach (var item in result)
{
Debug.WriteLine("{0}, {1}, {2}", item.Title, item.Id,item.Urls);
Debug.WriteLine(item.Urls.GetType());
foreach(var i in item.Urls){
Debug.WriteLine("i :"+i);
}
}
My desired output is -
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
But the output I am getting from the above piece of code is :
M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
I am getting the output as specified above and I am not quite sure why am I getting the above output. I desire to get the result for each 'Url' as is specified in the desired output. been fighting for a while to get the desired result.
Any help is appreciated. Novice programmer, new to C# not sure how to get the output. Is this because of the wrong XML format?
Your code is trying to call ToString() on a Linq expression which is why you're seeing that System.Linq.Enumerable+ gobbledygook.
You need to enumerate the Urls:
foreach (var item in result)
{
var i = 0;
foreach (var url in item.Urls)
{
Debug.WriteLine("{0}, {1}, {2}", item.Title, item.Id, url);
Debug.WriteLine(url.GetType());
Debug.WriteLine("i : " + i++);
}
}
or a more succinct way to write this:
result.ToList().ForEach(item =>
{
var i = 0;
foreach (var url in item.Urls)
{
Debug.WriteLine("{0}, {1}, {2}", item.Title, item.Id, url);
Debug.WriteLine(url.GetType());
Debug.WriteLine("i : " + i++);
}
});

SelectNodes give the empty node even if the xml Contains the node

XPathNavigator nav = xmlDoc.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace(string.Empty,#"http://www.w3.org/2005/Atom");
nsMgr.AddNamespace("dxp",#"http://schemas.google.com/analytics/2009");
nsMgr.AddNamespace("openSearch",#"http://a9.com/-/spec/opensearch/1.1/");
XmlNodeList nodeList = xmlDoc.SelectNodes("entry",nsMgr); // nodeList is empty why?
After execution of above code nodeList is empty
But when I see the XMLDocument it contains the the required Nodes entry
Here is the the XMLDocument innerXML
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dxp="http://schemas.google.com/analytics/2009" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&dimensions=ga:visitorType&metrics=ga:visitors&start-date=2012-10-06&end-date=2012-11-06</id>
<updated>2012-11-06T10:04:40.613Z</updated>
<title type="text">Google Analytics Data for Profile 63294209</title>
<link rel="self" type="application/atom+xml" href="https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&dimensions=ga:visitorType&metrics=ga:visitors&start-date=2012-10-06&end-date=2012-11-06" />
<author>
<name>Google Analytics</name>
</author>
<generator>Google Analytics</generator>
<openSearch:totalResults>2</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>1000</openSearch:itemsPerPage>
<dxp:aggregates>
<dxp:metric name="ga:visitors" type="integer" value="6709" />
</dxp:aggregates>
<dxp:containsSampledData>false</dxp:containsSampledData>
<dxp:dataSource>
<dxp:property name="ga:profileId" value="63294209" />
<dxp:property name="ga:webPropertyId" value="UA-34279407-1" />
<dxp:property name="ga:accountName" value="The Federal Savings Bank" />
<dxp:tableId>ga:63294209</dxp:tableId>
<dxp:tableName>The Federal Savings Bank</dxp:tableName>
</dxp:dataSource>
<dxp:endDate>2012-11-06</dxp:endDate>
<dxp:startDate>2012-10-06</dxp:startDate>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&ga:visitorType=New+Visitor&start-date=2012-10-06&end-date=2012-11-06</id>
<updated>2012-11-06T10:04:40.613Z</updated>
<title type="text">ga:visitorType=New Visitor</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
<dxp:dimension name="ga:visitorType" value="New Visitor" />
<dxp:metric name="ga:visitors" type="integer" value="5240" />
</entry>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&ga:visitorType=Returning+Visitor&start-date=2012-10-06&end-date=2012-11-06</id>
<updated>2012-11-06T10:04:40.613Z</updated>
<title type="text">ga:visitorType=Returning Visitor</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
<dxp:dimension name="ga:visitorType" value="Returning Visitor" />
<dxp:metric name="ga:visitors" type="integer" value="1469" />
</entry>
</feed>
There's a known issue in .NET with the default XML namespace - contrary to what is defined in the XML standards, in .NET you cannot use a string.Empty as the prefix - you need to use something else.
Try this:
XPathNavigator nav = xmlDoc.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace("def", #"http://www.w3.org/2005/Atom"); <== Give this a prefix!
nsMgr.AddNamespace("dxp", #"http://schemas.google.com/analytics/2009");
nsMgr.AddNamespace("openSearch", #"http://a9.com/-/spec/opensearch/1.1/");
XmlNodeList nodeList = xmlDoc.SelectNodes("/def:feed/def:entry", nsMgr);
Now, this list does have two nodes in it - right?

XmlDocument from google analytics to flat file c#

Using C# I have extracted data from Google Analytics Data API and have got it in an xmldocument. From that point I want to export it to flat file format (txt file). I am trying to use the xmlnode class to write out element attribute values within the 'entry' elements but cant do it. XML code below:
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dxp="http://schemas.google.com/analytics/2009" xmlns:openSearch="http://a9.com/- /spec/opensearch/1.1/">
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:xxxxxxxxx&dimensions=ga:date,ga:hostname&metrics=ga:visits,ga:newVisits,ga:visitBounceRate,ga:transactions,ga:transactionRevenue,ga:itemQuantity&start-date=2012-09-20&end-date=2012-09-26&start-index=1&max-results=1000</id>
<updated>2012-09-27T08:08:53.621Z</updated>
<title type="text">Google Analytics Data for Profile xxxxxxxxx</title>
<link rel="self" type="application/atom+xml" href="https://www.googleapis.com/analytics/v2.4/data?ids=ga:xxxxxxxxx&dimensions=ga:date,ga:hostname&metrics=ga:visits,ga:newVisits,ga:visitBounceRate,ga:transactions,ga:transactionRevenue,ga:itemQuantity&start-date=2012-09-20&end-date=2012-09-26&start-index=1&max-results=1000" />
<author>
<name>Google Analytics</name>
</author>
<generator>Google Analytics</generator>
<openSearch:totalResults>49</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>1000</openSearch:itemsPerPage>
<dxp:aggregates>
<dxp:metric name="ga:visits" type="integer" value="942258" />
<dxp:metric name="ga:newVisits" type="integer" value="393698" />
<dxp:metric name="ga:visitBounceRate" type="percent" value="26.960025810340692" />
<dxp:metric name="ga:transactions" type="integer" value="328" />
<dxp:metric name="ga:transactionRevenue" type="currency" value="427646.331187" />
<dxp:metric name="ga:itemQuantity" type="integer" value="322" />
</dxp:aggregates>
<dxp:containsSampledData>true</dxp:containsSampledData>
<dxp:dataSource>
<dxp:property name="ga:profileId" value="xxxxxxxxx" />
<dxp:property name="ga:webPropertyId" value="UA-33036832-1" />
<dxp:property name="ga:accountName" value="TCO-UK-TC.com-[thomascook.com]" />
<dxp:tableId>ga:xxxxxxxxx</dxp:tableId>
<dxp:tableName>ThomasCook UK - Analytics Extract (VP /search)</dxp:tableName>
</dxp:dataSource>
<dxp:endDate>2012-09-26</dxp:endDate>
<dxp:startDate>2012-09-20</dxp:startDate>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:xxxxxxxxx&ga:date=20120920&ga:hostname=87.248.112.8&start-date=2012-09-20&end-date=2012-09-26</id>
<updated>2012-09-27T08:08:53.621Z</updated>
<title type="text">ga:date=20120920 | ga:hostname=87.248.112.8</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
<dxp:dimension name="ga:date" value="20120920" />
<dxp:dimension name="ga:hostname" value="87.248.112.8" />
<dxp:metric name="ga:visits" type="integer" value="3" />
<dxp:metric name="ga:newVisits" type="integer" value="3" />
<dxp:metric name="ga:visitBounceRate" type="percent" value="100.0" />
<dxp:metric name="ga:transactions" type="integer" value="0" />
<dxp:metric name="ga:transactionRevenue" type="currency" value="0.0" />
<dxp:metric name="ga:itemQuantity" type="integer" value="0" />
</entry>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:xxxxxxxxx&ga:date=20120920&ga:hostname=alpha.thomascook.com&start-date=2012-09-20&end-date=2012-09-26</id>
<updated>2012-09-27T08:08:53.621Z</updated>
<title type="text">ga:date=20120920 | ga:hostname=alpha.thomascook.com</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
<dxp:dimension name="ga:date" value="20120920" />
<dxp:dimension name="ga:hostname" value="alpha.thomascook.com" />
<dxp:metric name="ga:visits" type="integer" value="160" />
<dxp:metric name="ga:newVisits" type="integer" value="109" />
<dxp:metric name="ga:visitBounceRate" type="percent" value="21.25" />
<dxp:metric name="ga:transactions" type="integer" value="0" />
<dxp:metric name="ga:transactionRevenue" type="currency" value="0.0" />
<dxp:metric name="ga:itemQuantity" type="integer" value="0" />
</entry>
....so on.....
In the code below I need to change it to locate the entry elements, but can't find it. Using "/entry" yields nothing.
XmlNamespaceManager xmlnsm = new XmlNamespaceManager(xmld.NameTable);
xmlnsm.AddNamespace("dxp", "http://schemas.google.com/analytics/2009");
xmlnsm.AddNamespace("openSearch", "http://a9.com/-/spec/opensearch/1.1/");
xmlnsm.AddNamespace("gd", "http://schemas.google.com/g/2005");
foreach (XmlNode xmlnd in xmld.SelectNodes("//dxp:dimension", xmlnsm))
{
DimVal = xmlnd.Attributes["value"].Value.ToString();
DimName = xmlnd.Attributes["name"].Value.Replace("ga:", "");
// ...
}
I could be missing a namespace. Can anyone please help me out?
XDocument xDoc = XDocument.Load(fileToLoad); //or XDocument.Parse(xmlString)
XNamespace dxp = "http://schemas.google.com/analytics/2009";
XNamespace ns = "http://www.w3.org/2005/Atom";
var result =
xDoc.Descendants(ns + "entry")
.Select(e => new
{
Dimension = e.Descendants(dxp + "dimension")
.ToDictionary(d => d.Attribute("name").Value, d => d.Attribute("value").Value),
Metric = e.Descendants(dxp + "metric")
.ToDictionary(d => d.Attribute("name").Value, d => d.Attribute("value").Value)
})
.ToList();
Print the result:
foreach(var entry in result)
{
foreach (var dim in entry.Dimension)
Console.WriteLine(dim.Key + " = " + dim.Value);
Console.WriteLine();
foreach (var met in entry.Metric)
Console.WriteLine(met.Key + " = " + met.Value);
Console.WriteLine("---------------");
}
If you're just interested in getting all of the dxp:dimension elements you could use
GetElementsByTagName("dimension", "http://schemas.google.com/analytics/2009")
instead of
SelectNodes("//dxp:dimension", xmlnsm)
If you're intent on using SelectNodes, I think you need another / ("///dxp:dimension").
Specifically for your case you can do:
foreach(XmlElement ele in xmld.GetElementsByTagName("element"))
{
foreach(XmlElement dim in ele.GetElementsByTagName("dimension", "http://schemas.google.com/analytics/2009")
{
// Do w/e w/ the element
}
foreach(XmlElement met in ele.GetElementsByTagName("metric", "http://schemas.google.com/analytics/2009")
{
// Do w/e w/ the element
}
}

Categories

Resources