XmlDocument from google analytics to flat file c# - 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
}
}

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.

Create an Envelope of the following XML

I am struggling with this situations for a while!! and i really hope i can get some help from you.
in a method i am receiving this XML as a string :
<?xml version="1.0" encoding="utf-16" standalone="no"?>
<VC_001_CreateDocument>
<VCRequest>
<Header>
<OrganisationData>
<ClientId />
<UserId />
<Pass />
</OrganisationData>
<Article>
<OutcomeSource>ERP</OutcomeSource>
<ArticleNumber>6034967-Sample</ArticleNumber>
<ProductNumbers>
<ProductNumber Type="GTIN" Level="PRI" />
</ProductNumbers>
<Forecast />
<ERPStatus>APP</ERPStatus>
<SerialisationFlag />
<CSDBArticleNumber>6034967-Sample</CSDBArticleNumber>
<ArticleDescription>Rose</ArticleDescription>
<WorkflowId />
<CommonName />
<PharmaceuticalForm />
<Strength />
<PackageWeight />
<PackageSize />
<PackageType />
<GS1GLN />
<GS1CompanyPrefix />
<Customer>
<CustomerId />
<CustomerErpNumber />
</Customer>
<ShelfLife />
<Region />
<ProductionSites>
<ProductionSite>
<ProductionSiteId />
<ProductionSiteErpNumber />
</ProductionSite>
</ProductionSites>
<GenericArticleField01 />
<GenericArticleField02 />
<GenericArticleField03 />
<GenericArticleField04 />
<GenericArticleField05 />
<RequiredFields>
<RequiredField Name="" />
</RequiredFields>
<Comment />
<Checked />
<TargetMarkets>
<TargetMarket>
<GS1NHRN />
<GenericFields Type="" Language="" />
<Mah />
<Wholesaler />
</TargetMarket>
</TargetMarkets>
<VerificationSystem />
<Email />
<FixData />
<StartValueInitial />
<SubPools>
<SubPool>
<AggregationLevel />
<PoolIdentProductNumber />
<QuantityPerLevel />
<IncompletePackagingRule />
<QuantityOfLayer />
<GenerationPattern />
<PostProductionSerialNumberAssignment />
<PrePrinting />
<Factor />
<Threshold />
<SerialNumberType />
<ExtensionDigit />
<SerialNumberSource />
<DeliveranceInformation>
<MinimumValue />
<PercentualAmount />
</DeliveranceInformation>
</SubPool>
</SubPools>
<Report>
<ReportExternal />
<ReportVerificationSystem />
</Report>
</Article>
</Header>
</VCRequest>
</VC_001_CreateDocument>
to transform in an Envelope to use in my SOAP Request exactly like this: reading the node values of course if they have!
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:csdb="http://site.de/csdb">
<soapenv:Header/>
<soapenv:Body>
<csdb:VC_001_CreateDocument>
<csdb:VARequest>
<csdb:Header>
<csdb:OrganisationData>
<csdb:ClientId>EDP</csdb:ClientId>
<csdb:UserId>webservice</csdb:UserId>
<csdb:Pass>!password*</csdb:Pass>
</csdb:OrganisationData>
</csdb:Header>
<csdb:Article>
<csdb:OutcomeSource>ERP</csdb:OutcomeSource>
<csdb:ArticleNumber>6034967-Sample</csdb:ArticleNumber>
<csdb:ProductNumbers>
<csdb:ProductNumber type="GTIN" level="PRI"></csdb:ProductNumber>
</csdb:ProductNumbers>
<csdb:Forecast></csdb:Forecast>
<csdb:ERPStatus></csdb:ERPStatus>
<csdb:SerialisationFlag></csdb:SerialisationFlag>
<csdb:CSDBArticleNumber>6034967-AMOSTRA</csdb:CSDBArticleNumber>
<csdb:ArticleDescription>PINOX ROSA</csdb:ArticleDescription>
<csdb:WorkflowId></csdb:WorkflowId>
<csdb:CommonName></csdb:CommonName>
<csdb:PharmaceuticalForm></csdb:PharmaceuticalForm>
<csdb:Strength></csdb:Strength>
<csdb:PackageWeight></csdb:PackageWeight>
<csdb:PackageSize></csdb:PackageSize>
<csdb:PackageType></csdb:PackageType>
<csdb:GS1GLN></csdb:GS1GLN>
<csdb:GS1CompanyPrefix></csdb:GS1CompanyPrefix>
<csdb:Customer>
<csdb:Customer></csdb:Customer>
<csdb:CustomerId></csdb:CustomerId>
<csdb:CustomerErpNumber></csdb:CustomerErpNumber>
</csdb:Customer>>
<csdb:ShelfLife></csdb:ShelfLife>
<csdb:Region></csdb:Region>
<csdb:ProductionSites>
<csdb:ProductionSite>
<csdb:ProductionSiteId></csdb:ProductionSiteId>
<csdb:ProductionSiteErpNumber></csdb:ProductionSiteErpNumber>
</csdb:ProductionSite>
</csdb:ProductionSites>
<csdb:GenericArticleField01></csdb:GenericArticleField01>
<csdb:GenericArticleField02></csdb:GenericArticleField02>
<csdb:GenericArticleField03></csdb:GenericArticleField03>
<csdb:GenericArticleField04></csdb:GenericArticleField04>
<csdb:GenericArticleField05></csdb:GenericArticleField05>
<csdb:RequiredFields>
<csdb:RequiredField name="?"></csdb:RequiredField>
</csdb:RequiredFields>
<csdb:Comment></csdb:Comment>
<csdb:Checked></csdb:Checked>
<csdb:GS1NHRN GS1NHRNNational="?"></csdb:GS1NHRN>
<csdb:TargetMarkets>
<csdb:TargetMarket targetMarket="?">
<csdb:GS1NHRN GS1NHRNNational="?"></csdb:GS1NHRN>
<csdb:GenericFields type="?" language="?"></csdb:GenericFields>
<csdb:Mah></csdb:Mah>
<csdb:Wholesaler></csdb:Wholesaler>
</csdb:TargetMarket>
</csdb:TargetMarkets>
<csdb:Verificationsystem></csdb:Verificationsystem>
<csdb:Email></csdb:Email>
<csdb:FixData></csdb:FixData>
<csdb:StartValueInitial></csdb:StartValueInitial>
<csdb:Subpools>
<csdb:Subpool>
<csdb:AggregationLevel></csdb:AggregationLevel>
<csdb:PoolIdentProductNumber type="?"></csdb:PoolIdentProductNumber>
<csdb:QuantityPerLevel></csdb:QuantityPerLevel>
<csdb:IncompletePackagingRule></csdb:IncompletePackagingRule>
<csdb:QuantityOfLayer></csdb:QuantityOfLayer>
<csdb:GenerationPattern></csdb:GenerationPattern>
<csdb:PostProductionSerialNumberAssignment></csdb:PostProductionSerialNumberAssignment>
<csdb:PrePrinting></csdb:PrePrinting>
<csdb:Factor></csdb:Factor>
<csdb:Threshold></csdb:Threshold>
<csdb:SerialNumberType></csdb:SerialNumberType>
<csdb:ExtensionDigit></csdb:ExtensionDigit>
<csdb:SerialNumberSource></csdb:SerialNumberSource>
<csdb:DeliveranceInformation>
<csdb:MinimumValue></csdb:MinimumValue>
<csdb:PercentualAmount></csdb:PercentualAmount>
</csdb:DeliveranceInformation>
</csdb:Subpool>
</csdb:Subpools>
<csdb:Report>
<csdb:ReportExternal></csdb:ReportExternal>
<csdb:ReportVerificationSystem></csdb:ReportVerificationSystem>
</csdb:Report>
</csdb:Article>
</csdb:VCRequest>
</csdb:VC_001_CreateDocument>
</soapenv:Body>
</soapenv:Envelope>
THis is exactly the XML that i receive and that's the envelope that i will have to create (Request generated by SOAPUI),the nodes will have dynamic Values of course, those parameteres were just an example.
i assume this is the easiest way since i can't "add service reference" because i am trying to develop a DLL where the webservices will be nested. There's no web.config where this DLL is going to be installed.
Thank you soo much for helping me with the logic for this problem that is driving me crazy
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
reader.ReadLine(); // skip the utf-16 in header that Net Library doesn't accept
XDocument doc = XDocument.Load(reader);
string soapHeader = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:csdb=\"http://site.de/csdb\">" +
"<soapenv:Header/>" +
"<soapenv:Body>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
XElement soap = XElement.Parse(soapHeader);
XNamespace nsCsdb = soap.GetNamespaceOfPrefix("csdb");
XNamespace nsSoapenv = soap.GetNamespaceOfPrefix("soapenv");
XElement body = soap.Descendants(nsSoapenv + "Body").FirstOrDefault();
body.Add(doc.Root);
foreach (XElement child in body.Descendants())
{
child.Name = nsCsdb.GetName(child.Name.LocalName);
List<XAttribute> atList = child.Attributes().ToList();
child.Attributes().Remove();
foreach (XAttribute at in atList)
child.Add(new XAttribute(nsCsdb.GetName(at.Name.LocalName), at.Value));
}
}
}
}

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);

c# linq selection of Element by selecting an child.child.attribute

I want to select one < NodeLabel > Tag. Selection should be done, when the < NodeLabel > has Elements.
The xml File looks like
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:java="http://www.yworks.com/xml/yfiles-common/1.0/java" xmlns:sys="http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0" xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd">
<!--Created by yEd 3.14.2-->
<key attr.name="description" attr.type="string" for="graph" id="d0" />
<key for="port" id="d1" yfiles.type="portgraphics" />
<key for="port" id="d2" yfiles.type="portgeometry" />
<key for="port" id="d3" yfiles.type="portuserdata" />
<key attr.name="url" attr.type="string" for="node" id="d4" />
<key attr.name="description" attr.type="string" for="node" id="d5" />
<key for="node" id="d6" yfiles.type="nodegraphics" />
<key for="graphml" id="d7" yfiles.type="resources" />
<key for="edge" id="d8" yfiles.type="portconstraints" />
<key attr.name="url" attr.type="string" for="edge" id="d9" />
<key attr.name="description" attr.type="string" for="edge" id="d10" />
<key for="edge" id="d11" yfiles.type="edgegraphics" />
<graph edgedefault="directed" id="G">
<data key="d0" />
<node id="n0" yfiles.foldertype="group">
<data key="d6">
<y:TableNode configuration="YED_TABLE_NODE">
<y:Geometry height="646.8359897640048" width="1012.9110704527719" x="0.0" y="-646.8359897640048" />
<y:Fill color="#ECF5FF" color2="#0042F440" transparent="false" />
<y:BorderStyle color="#000000" type="line" width="1.0" />
<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="15" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="22.37646484375" modelName="internal" modelPosition="t" textColor="#000000" visible="true" width="230.7578125" x="391.07662897638596" y="4.0">title</y:NodeLabel>
<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="54.712890625" x="111.14444510514247" y="33.0">col0<y:LabelModel><y:ColumnNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:ColumnNodeLabelModelParameter id="column_0" inside="true" verticalPosition="0.0" /></y:ModelParameter></y:NodeLabel>
<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="72.712890625" x="482.3174666703262" y="33.0">col1<y:LabelModel><y:ColumnNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:ColumnNodeLabelModelParameter id="column_1" inside="true" verticalPosition="0.0" /></y:ModelParameter></y:NodeLabel>
<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="58.046875" x="869.6051192915697" y="33.0">col2<y:LabelModel><y:ColumnNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:ColumnNodeLabelModelParameter id="column_2" inside="true" verticalPosition="0.0" /></y:ModelParameter></y:NodeLabel>
<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" rotationAngle="270.0" textColor="#000000" visible="true" width="40.01171875" x="3.0" y="330.4121355070024">row0<y:LabelModel><y:RowNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:RowNodeLabelModelParameter horizontalPosition="0.0" id="row_0" inside="true" /></y:ModelParameter></y:NodeLabel>
<y:StyleProperties>
...
I just want to select < NodeLabel > Tag with the descendants("y:RowNodeLabelModelParameter").attribute("id").value = "row_0"
So far so easy.
I tried with the following code
XElement TableNode = yedFile.Descendants(y + "TableNode").FirstOrDefault();
XElement NodeLblRow = new XElement(TableNode.Elements(y + "NodeLabel")
.Where(t => t.HasElements )
.Where(x => x.Element(y + "ModelParameter")
.Element(y + "RowNodeLabelModelParameter")
.Attribute("id").Value == "row_0"
).FirstOrDefault()
);
but in the second where I get an NullReferenceException. What is missing? These are my first tries with linq and c# so it may be that I'am thinking to complicated. I think the problem occurs, because the first < NodeLabel > Tag has no child's. That's why I divided the selection in to "Where". The first one should select only the < NodeLabel > Tag's with child's an the second one should exactly match the last < NodeLabel > Tag. So far my intention.
Can someone give me a hint?
Thank you very much for your ideas and help.
For information:
I use the following namespaces
XNamespace xmlns = XNamespace.Get("http://graphml.graphdrawing.org/xmlns");
XNamespace /*xmlns:*/java = XNamespace.Get("http://www.yworks.com/xml/yfiles-common/1.0/java");
XNamespace /*xmlns:*/sys = XNamespace.Get("http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0");
XNamespace /*xmlns:*/x = XNamespace.Get("http://www.yworks.com/xml/yfiles-common/markup/2.0");
XNamespace /*xmlns:*/xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace /*xmlns:*/y = XNamespace.Get("http://www.yworks.com/xml/graphml");
XNamespace /*xmlns:*/yed = XNamespace.Get("http://www.yworks.com/xml/yed/3");
You will get a null reference exception when you come across an element that doesn't have elements or attributes that make up your query.
By sticking with IEnumerable rather than 'exiting the monad' and making use of explicit conversions you can avoid having to do null checks, for example:
var element = yedFile.Descendants(y + "NodeLabel")
.FirstOrDefault(e => (string)e.Elements(y + "ModelParameter")
.Elements(y + "RowNodeLabelModelParameter")
.Attributes("id")
.SingleOrDefault() == "row_0");

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?

Categories

Resources