C# XPathSelectElements returns null? - c#

I have an XML document:
<xsd:form-definition xmlns:xsd="http://...m.xsd"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="....xsd" ...>
<xsd:page>
<xsd:formant source-name="label" id="guid1" />
<xsd:formant source-name="label id="guid2" />
<xsd:formant source-name="label" id="guid3">
<xsd:value>2013-04-24</xsd:value>
</xsd:formant>
</xsd:page>
</xsd:form-definition>
and by C# code I want to iterate through the specific elements and get the id attribute and value (if exist) - lets say labels.
To do so, I try the code
XDocument xml = (document load);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("f", "http://m.xsd");
foreach (XElement e in xml.XPathSelectElements("//f:formant[#source-name = 'label']", ns))
{
....
}
but the foreach loop does not return any elements. Why ?

It works for me. Check that your namespaces f and xsd match exactly. In your example they don't match. Also, there are some other syntax errors in your example, e.g. the source-name value of the second formant doesn't end with a double quote.
XDocument xml = XDocument.Parse(
#"<xsd:form-definition xmlns:xsd=""http://m.xsd""
xmlns:ds=""http://www.w3.org/2000/09/xmldsig#""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<xsd:page>
<xsd:formant source-name=""label"" id=""guid1"" />
<xsd:formant source-name=""label2"" id=""guid2"" />
<xsd:formant source-name=""label"" id=""guid3"">
<xsd:value>2013-04-24</xsd:value>
</xsd:formant>
</xsd:page>
</xsd:form-definition>");
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("f", "http://m.xsd");
foreach (XElement e in xml.XPathSelectElements(
"//f:formant[#source-name = 'label']", ns))
{
Console.WriteLine(e);
}
Console.ReadLine();

Related

xml descendants looping issue in c#

I am unable to get values from xml while looping, i want to get values of each and every element inside HostedService. For e.g. service name, Url etc.
Below is my code
XDocument doc;
using (Stream input = System.IO.File.OpenRead(#"D:\Test.xml"))
{
doc = XDocument.Load(input);
foreach (var events in doc.Root.Descendants("HostedServices")) // loop through all events
{
}
}
XML example:
<HostedServices xmlns="http://schemas.microsoft.com/windowsazure"xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HostedService>
<Url>https://www.google.com</Url>
<ServiceName>sharepoint2013vm</ServiceName>
<HostedServiceProperties>
<Description i:nil="true" />
<Location>East Asia</Location>
<Label>c2hhcmVwb2ludDIwMTN2bQ==</Label>
<Status>Created</Status>
<DateCreated>2015-01-13T03:42:21Z</DateCreated>
<DateLastModified>2015-01-13T03:42:46Z</DateLastModified>
<ExtendedProperties>
<ExtendedProperty>
<Name>ResourceGroup</Name>
<Value>sharepoint2013vm</Value>
</ExtendedProperty>
<ExtendedProperty>
<Name>ResourceLocation</Name>
<Value>East Asia</Value>
</ExtendedProperty>
</ExtendedProperties>
</HostedServiceProperties>
</HostedService>
<HostedService>
</HostedService>
</HostedServices>
Here is one way to do it.
XDocument doc;
using (Stream input = System.IO.File.OpenRead("XMLFile1.xml"))
{
doc = XDocument.Load(input);
XmlNamespaceManager nm = new XmlNamespaceManager(new NameTable());
XNamespace ns = doc.Root.GetDefaultNamespace();
nm.AddNamespace("ns", ns.NamespaceName);
foreach (var hostedService in doc.Root.XPathSelectElements("ns:HostedService",nm)) // loop through all events
{
if (hostedService.XPathSelectElement("ns:ServiceName", nm) != null)
{
var service = hostedService.XPathSelectElement("ns:ServiceName",nm).Value;
}
if (hostedService.XPathSelectElement("ns:Url",nm) != null)
{
var url = hostedService.XPathSelectElement("ns:Url",nm).Value;
}
}
}
Use this:
XDocument doc = XDocument.Load(#"D:\Test.xml");
to load the XML file in memory.
You have to use XNamespace since your original XML declares one:
XNamespace ns = "http://schemas.microsoft.com/windowsazure";
To loop through all elements of <HostedService> use this:
foreach (var events in doc.Descendants(ns+"HostedService").Elements())
{
}
The above foreach will give you access to all child Elements() of <HostedService>, i.e.
<Url>
<ServiceName> and
<HostedServiceProperties>
If, on the other side, you want to access all elements (including child elements etc.) below <HostedService> then use the following ;
foreach (var events in doc.Descendants(ns + "HostedService").DescendantNodes().OfType<XElement>())
{
}
The above gives you access to:
<Url>
<ServiceName>
<HostedServiceProperties>
<Description>
<Location>
<Label>
... etc.
You need to specify XML namespace with your element name:
XNamespace ns = "http://schemas.microsoft.com/windowsazure";
foreach (var events in doc.Root.Descendants(ns + "HostedServices"))
{
}
Have a look at Working with XML Namespaces for more info.

Use XPath with XML namespace

I want to process the xml below with XPath:
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="Cloud" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2014-01.2.3">
<Role name="Worker">
<Instances count="2" />
<ConfigurationSettings>
<Setting name="setting1" value="value1" />
<Setting name="setting2" value="value2" />
</ConfigurationSettings>
<Certificates>
</Certificates>
</Role>
</ServiceConfiguration>
Tere's a xmlns for the root element.
My code is this:
XElement doc = XElement.Load(xmlFilePath);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("//prefix:ServiceConfiguration/Role/ConfigurationSettings/Setting[1]", ns); // <===== always return null.
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
But the settingDiagConnectionString is always null.
Why?
Add 1
Thanks har07.
After adding prefix for every element, the following code works:
XmlDocument doc = new XmlDocument();
doc.Load(cscfgPath);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XmlNode settingDiagConnectionString = doc.SelectNodes("/prefix:ServiceConfiguration/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns)[0];
settingDiagConnectionString.Attributes["value"].Value = "hello,world!";
But the following code still don't work. The settingDiagConnectionString is still null. Why?
XElement doc = XElement.Load(cscfgPath);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("/prefix:ServiceConfiguration/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns);
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
Default namespace has different nature. The element where the default namespace declared and all of it's descendant without different namespace declaration considered in the same default namespace. Therefore, you need to use the prefix for all descendats as well. This XPath worked fine for me :
XElement doc = XElement.Parse(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("//prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns);
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
UPDATE :
Responding to your update. That's because you're using XElement. In this case, doc it self already represents <ServiceConfiguration> element so you don't need to include "ServiceConfiguration" in the XPath :
/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]
..or use XDocument instead of XElement if you want to make it work using the same XPath as for XmlDocument.

Linq-to-Xml prefix parse error

I am currently trying to learn how to parse data, and its a bit confusing. can someone check out my code and see what I'm doing wrong or if im even heading in the right direction.
XML File:
<xml xmlns:a='BLAH'
xmlns:b='BLAH'
xmlns:c='BLAH'
xmlns:d='BLAH'>
<a:info>
<b:cat Option1='blah' Option2='blah' Option3='blah' />
</a:info>
</xml>
C# Code:
XmlDocument doc = new XmlDocument();
doc.Load(richTextBox2.Text);
XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("a", "BLAH");
man.AddNamespace("b", "BLAH");
man.AddNamespace("c", "BLAH");
man.AddNamespace("d", "BLAH");
XmlNode temps = doc.SelectSingleNode("/a:info/b:cat/Option1/", man);
richTextBox1.Text = temps.InnerText;
I am new to C#, I cant find a good example explaining how to successfully use loops to find more then one:
<b:chat />
You are using the wrong API if you're looking for LINQ to XML. Use the XDocument class instead.
Assume the following input XML-document (please note namespace URLs):
<xml xmlns:a='http://localhost/scheme_a'
xmlns:b='http://localhost/scheme_b'
xmlns:c='http://localhost/scheme_c'
xmlns:d='http://localhost/scheme_d'>
<a:info>
<b:cat Option1='1' Option2='1' Option3='1' />
</a:info>
<a:info>
<b:cat Option1='2' Option2='2' Option3='2' />
</a:info>
</xml>
There are the ways to get all <b:chat /> elements.
XmlDocument class:
var xmlDocument = new XmlDocument();
xmlDocument.Load(...);
var xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
xmlNamespaceManager.AddNamespace("a", "http://localhost/scheme_a");
xmlNamespaceManager.AddNamespace("b", "http://localhost/scheme_b");
xmlNamespaceManager.AddNamespace("c", "http://localhost/scheme_c");
xmlNamespaceManager.AddNamespace("d", "http://localhost/scheme_d");
var bCatNodes = xmlDocument.SelectNodes("/xml/a:info/b:cat", xmlNamespaceManager);
var option1Attributes = bCatNodes.Cast<XmlNode>().Select(node => node.Attributes["Option1"]);
// Also, all Option1 attributes can be retrieved directly using XPath:
// var option1Attributes = xmlDocument.SelectNodes("/xml/a:info/b:cat/#Option1", xmlNamespaceManager).Cast<XmlAttribute>();
LINQ to XML XDocument Class. XName can be passed with a namespace to Descendants() and Element() methods.
Use Descendants() to get all <b:chat /> elements.
var xDocument = XDocument.Load(...);
XNamespace xNamespace = "http://localhost/scheme_b";
var xElements = xDocument.Descendants(xNamespace + "cat");
// For example, get all the values of Option1 attribute for the b:chat elements:
var options1 = xElements.Select(element => element.Attribute("Option1")).ToList();

reading node from xml file in XMLDocument

i am trying to grab the TopicName how should i go after it and try different combination but somehow i am unable to get TopicName below is my source codee...
XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing
xdoc.Load(
"http://latestpackagingnews.blogspot.com/feeds/posts/default"
);//loading XML in xml doc
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("content");//reading node so that we can traverse thorugh the XML
foreach (XmlNode xNode in xNodelst)//traversing XML
{
//litFeed.Text += "read";
}
sample xml file
<content type="application/xml">
<CatalogItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="sitename.xsd">
<CatalogSource Acronym="ABC" OrganizationName="ABC Corporation" />
<CatalogItem Id="3212" CatalogUrl="urlname">
<ContentItem xmlns:content="sitename.xsd" TargetUrl="url">
<content:SelectionSpec ClassList="" ElementList="" />
<content:Language Value="eng" Scheme="ISO 639-2" />
<content:Source Acronym="ABC" OrganizationName="ABC Corporation" />
<content:Topics Scheme="ABC">
<content:Topic TopicName="Marketing" />
<content:Topic TopiccName="Coverage" />
</content:Topics>
</ContentItem>
</CatalogItem>
</CatalogItems>
</content>
The Topic nodes in your XML are using the content namespace - you need to declare and use the XML namespace in your code, then you can use SelectNodes() to grab the nodes of interest - this worked for me:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("content", "sitename.xsd");
var topicNodes = xdoc.SelectNodes("//content:Topic", nsmgr);
foreach (XmlNode node in topicNodes)
{
string topic = node.Attributes["TopicName"].Value;
}
Just as a comparison see how easy this would be with Linq to XML:
XDocument xdoc = XDocument.Load("test.xml");
XNamespace ns = "sitename.xsd";
string topic = xdoc.Descendants(ns + "Topic")
.Select(x => (string)x.Attribute("TopicName"))
.FirstOrDefault();
To get all topics you can replace the last statement with:
var topics = xdoc.Descendants(ns + "Topic")
.Select(x => (string)x.Attribute("TopicName"))
.ToList();
If you just need a specific element, then I'd use XPath:
This is a guide to use XPath in C#:
http://www.codeproject.com/KB/XML/usingXPathNavigator.aspx
And this is the query that will get you a collection of your Topics:
//content/CatalogItems/CatalogItem/ContentItem/content:Topics/content:Topic
You could tweak this query depending on what it is you're trying to accomplish, grabbing just a specific TopicName value:
//content/CatalogItems/CatalogItem/ContentItem/content:Topics/content:Topic/#TopicName
XPath is pretty easy to learn. I've done stuff like this pretty quickly with no prior knowledge.
You can paste you XML and xpath query here to test your queries:
http://www.bit-101.com/xpath/
The following quick and dirty LINQ to XML code obtains your TopicNames and prints them on the console.
XDocument lDoc = XDocument.Load(lXmlDocUri);
foreach (var lElement in lDoc.Element("content").Element(XName.Get("CatalogItems", "sitename.xsd")).Elements(XName.Get("CatalogItem", "sitename.xsd")))
{
foreach (var lContentTopic in lElement.Element(XName.Get("ContentItem", "sitename.xsd")).Element(XName.Get("Topics", "sitename.xsd")).Elements(XName.Get("Topic", "sitename.xsd")))
{
string lTitle = lContentTopic.Attribute("TopicName").Value;
Console.WriteLine(lTitle);
}
}
It'd have been a lot shorter if it wasn't for all the namespaces :) (Instead of "XName.Get" you would just use the name of the element).

Using XPath in C# for get all the node values from XML

I have got XML below:
<tcm:Component ID="tcm:481-636667" IsEditable="false" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<tcm:Context>
<tcm:Publication xlink:type="simple" xlink:title="07 Internal Test Publication" xlink:href="tcm:0-481-1"/>
<tcm:OrganizationalItem xlink:type="simple" xlink:title="System Resources" xlink:href="tcm:481-92640-2"/>
</tcm:Context>
<tcm:Data>
<tcm:Title>IBE - Skywards</tcm:Title>
<tcm:Type>Normal</tcm:Type>
<tcm:Schema xlink:type="simple" xlink:title="Resources" xlink:href="tcm:481-190471-8"/>
<tcm:Content>
<Resources xmlns="http://www.sdltridion.com/tridion/schemas">
<Text>
<Key>SKYRL_MBD</Key>
<Value>Miles Breakdown</Value>
</Text>
<Text>
<Key>ltSR_MB.Text</Key>
<Value>View Miles Breakdown</Value>
</Text>
<Text>
<Key>ltSR_HMB.Text</Key>
<Value>Hide Miles Breakdown</Value>
</Text>
<Text>
<Key>SKYRL_MBD_LK</Key>
<Value>Miles Breakdown</Value>
</Text>
</Resources>
</tcm:Content>
<tcm:Metadata>
<Metadata xmlns="http://www.sdltridion.com/tridion/schemas">
<Language>
<Language>English</Language>
</Language>
</Metadata>
</tcm:Metadata>
</tcm:Data>
</tcm:Component>
Now I want to write a method in C# which will take this XML as input and will return all the "Key" and "Value" data in List.
Please suggest.
First, declare the lists to keep values:
using System.Collections.Generic;
List<string> keysList = new List<string>();
List<string> valuesList = new List<string>();
Then:
using System.Xml; // System.Xml.dll
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml); // Load(file)
var ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");
ns.AddNamespace("xlink", "http://www.w3.org/1999/xlink");
foreach (XmlNode node in doc.SelectNodes("//*[local-name()=\"Key\"]"))
{
keysList.Add(node.InnerText);
}
foreach (XmlNode node in doc.SelectNodes("//*[local-name()=\"Value\"]"))
{
valuesList.Add(node.InnerText);
}
if you don't need XML DOM, only XPath to evaluate:
using System.Xml.XPath; // System.Xml.dll
XPathDocument doc = null;
using (TextReader reader = new StringReader(xml))
{
doc = new XPathDocument(reader); // specify just path to file if you have such one
}
XPathNavigator nav = doc.CreateNavigator();
foreach (XPathNavigator node in (XPathNodeIterator)nav.Evaluate("//*[local-name()=\"Key\"]"))
{
keysList.Add(node.Value);
}
foreach (XPathNavigator node in (XPathNodeIterator)nav.Evaluate("//*[local-name()=\"Value\"]"))
{
valuesList.Add(node.Value);
}
Use XElement or XDocument ( Linq2Xml )
XElement xml = XElement.Parse("inputxml");
var keys = xml.Descendants("Key");
OP says he uses .Net 2.0. Then this won't work!
Have a look at the classes in the System.Xml.Linq namespace. If you start with an XDocument you can load your XML and just query the contents using Linq.

Categories