Access Child nodes with namespace using xpath - c#

How can I read the content of the childnotes using Xpath?
I have already tried this:
var xml = new XmlDocument();
xml.Load("server-status.xml");
var ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("ns", "namespace");
var node = xml.SelectSingleNode("descendant::ns:server[ns:ip-address]", ns)
Console.WriteLine(node.InnerXml)
But I only get a string like this:
<ip-address>127.0.0.50</ip-address><name>Server 1</name><group>DATA</group>
How can I get the values individually?
Xml file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<server-status xmlns="namespace">
<server>
<ip-address>127.0.0.50</ip-address>
<name>Server 1</name>
<group>DATA</group>
</server>
</server-status>

You're using XML namespaces in XPath correctly.
However, your original XPath,
descendant::ns:server[ns:ip-address]
says to select all ns:server elements with ns:ip-address children.
If you wish to select the ns:ip-address children themselves, instead use
descendant::ns:server/ns:ip-address
Similarly, you could select ns:name or ns:group elements.

Related

How to add Namespace and Declaration to the existing XML

how to add Namespace and Declaration to the existing xml.
My XML
<Order>
<child1></child1>
</Order>
Expected
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Order xmlns="http://a.com/a">
<child1></child>
</Order>
Assuming you're only changing the element namespaces and ignoring any attributes, you can parse this using LINQ to XML and then change every element's qualified name:
var doc = XDocument.Parse(xml);
XNamespace ns = "http://a.com/a";
foreach (var element in doc.Descendants())
{
element.Name = ns + element.Name.LocalName;
}

Change the inner text of Xml file using c#

I have the following XML file which has the namespace as shown...
I have to change the inner text of XML file but need suggestion how to do it.
My XML file is:
<?xml version="1.0"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<buttons>
<workshop1>Google</workshop1>
<workshop1>Yahoo</workshop1>
<url1>www.google.co.uk</url1>
</buttons>
</Report>
I have to change the inner text of second node workshop1 from "Yahoo" to "new".
Do this using XElement.
The "problem" you probably had is due to the namespace this xml has:
XElement xml = getXml(); // get your xml from a service\file\ftp etc..
XNamespace ns = "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition";
xml.Descendants(ns + "workshop1").First().Value = "new";
Your output:
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<buttons>
<workshop1>new</workshop1>
<url1>www.google.co.uk</url1>
</buttons>
</Report>
For more than one node use this:
XNamespace ns = "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition";
var nodes = xml.Descendants(ns + "workshop1").ToList();
nodes[0].Value = "new";
nodes[1].Value = "new2";
// etc....

Unable to use XmlDocument.SelectSingleNode on XML with Two Namespaces

I'm trying to parse the following XML:
<?xml version="1.0" encoding="utf-8"?>
<A2AAnf:MPPPPPP xsi:schemaLocation="urn:A2AAnf:xsd:$MPPPPPP.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:A2AAnf="urn:A2AAnf:xsd:$MPPPPPP">
<A2AAnf:Num>0</A2AAnf:Num>
<A2AAnf:FIT xmlns="urn:iso:std:iso:20022:xsd:003.001">
<Hdr>
<Inf>
<Mtd>TEST</Mtd>
</Inf>
</Hdr>
</A2AAnf:FIT>
I want to access the <Mtd> tag.
XMLQuire shows the path to be /A2AAnf:MPPPPPP/A2AAnf:FIT/dft:Hdr/dft:Inf/dft:Mtd, but when I'm trying to parse it using the following code:
XmlDocument xmldocument = new XmlDocument();
var xmlNameSpaceManager = new XmlNamespaceManager(xmldocument.NameTable);
xmlNameSpaceManager.AddNamespace("A2AAnf", "urn:A2AAnf:xsd:$MPPPPPP");
try
{
xmldocument.LoadXml(m_XML);
var node = xmldocument.SelectSingleNode("/A2AAnf:MPPPPPP/A2AAnf:FIT/dft:Hdr/dft:Inf/dft:Mtd", xmlNameSpaceManager);
}
I receive the following error:
namespace prefix 'dft' is not defined
And since I can't find "dft" in my XML, I also tried to remove the "dft" prefix and search for the same string without "dft". This time, nothing was returned.
What am I missing?
You have to add dft to your XmlNamespaceManager:
var xmlNameSpaceManager = new XmlNamespaceManager(xmldocument.NameTable);
xmlNameSpaceManager.AddNamespace("A2AAnf", "urn:A2AAnf:xsd:$MPPPPPP");
xmlNameSpaceManager.AddNamespace("dft", "urn:iso:std:iso:20022:xsd:003.001");
The prefixes you use in your XPath query have nothing to do with the prefixes used in the XML document. They are instead the prefixes you define in your XmlNamespaceManager.

Not able to process XML file that contains namespace

I have an xml file whose structure goes as under(partial)
<?xml version="1.0" encoding="UTF-8"?>
<TestRun id="ee3838c9-a7e2-4ddf-acb1-58589e39422d" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestSettings name="LocalSettings" id="e445106a-c672-4959-94d3-ec8cef9ac7e4">
<Results>
<UnitTestResult executionId="0e790a10-105f-44b1-a8f7-f72709651c17" testId="ae349466-4276-cfa9-908c-026a8589473b" testName="ValidateEmailAddressAndCompanyCode7" computerName="ACCUREVDEV" duration="00:00:41.4297842" startTime="2013-02-27T23:18:52.0238567-08:00" endTime="2013-02-27T23:19:34.0057439-08:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="0e790a10-105f-44b1-a8f7-f72709651c17">
</UnitTestResult>
<UnitTestResult executionId="e9e7679d-fc39-43b7-a096-819f054a3795" testId="1a808be5-21a5-37c4-5892-2969707ae42f" testName="AccountExtensionSubscriptionWithOneMachineAndThreeSubscriptionsTest_withExpiredMachines" computerName="ACCUREVDEV" duration="00:00:56.1243356" startTime="2013-02-27T23:19:34.0174655-08:00" endTime="2013-02-27T23:20:30.8418287-08:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="e9e7679d-fc39-43b7-a096-819f054a3795">
</UnitTestResult> .............................
...............................
As can be make out that, there is a namespace involved
xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
Now if I do
string source = #"D:\23_18_43.trx";
XDocument xDoc = XDocument.Load(source);
var xxxx = (from data in xDoc.Descendants("Results")
select data).Descendants("UnitTestResult").ToList();
There is no value coming.
Whereas , if I omit the namespace and do my processing, it works.
How can I proceed without removing the namespace explicitly from the source file? Can it be done programatically?
Thanks
To find a node in XML with a default namespace, you need to still search for that node using the namespace.
Eg.
XNamespace defaultNs = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
and then in your "query";
var xxxx = (from data in xDoc.Descendants(defaultNs + "Results")
select data).Descendants(defaultNs +"UnitTestResult").ToList();

How to correctly parse an XML document with arbitrary namespaces

I am trying to parse somewhat standard XML documents that use a schema called MARCXML from various sources.
Here are the first few lines of an example XML file that needs to be handled...
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<marc:record>
<marc:leader>00925njm 22002777a 4500</marc:leader>
and one without namespace prefixes...
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<collection xmlns="http://www.loc.gov/MARC21/slim">
<record>
<leader>01142cam 2200301 a 4500</leader>
Key point: in order to get the XPaths to resolve further along in the program I have to go through a regex routine to add the namespaces to the NameTable (which doesn't add them by default). This seems unnecessary to me.
Regex xmlNamespace = new Regex("xmlns:(?<PREFIX>[^=]+)=\"(?<URI>[^\"]+)\"", RegexOptions.Compiled);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlRecord);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
MatchCollection namespaces = xmlNamespace.Matches(xmlRecord);
foreach (Match n in namespaces)
{
nsMgr.AddNamespace(n.Groups["PREFIX"].ToString(), n.Groups["URI"].ToString());
}
The XPath call looks something like this...
XmlNode leaderNode = xmlDoc.SelectSingleNode(".//" + LeaderNode, nsMgr);
Where LeaderNode is a configurable value and would equal "marc:leader" in the first example and "leader" in the second example.
Is there a better, more efficient way to do this? Note: suggestions for solving this using LINQ are welcome, but I would mainly like to know how to solve this using XmlDocument.
EDIT: I took GrayWizardx's advice and now have the following code...
if (LeaderNode.Contains(":"))
{
string prefix = LeaderNode.Substring(0, LeaderNode.IndexOf(':'));
XmlNode root = xmlDoc.FirstChild;
string nameSpace = root.GetNamespaceOfPrefix(prefix);
nsMgr.AddNamespace(prefix, nameSpace);
}
Now there's no more dependency on Regex!
If you know there is going to be a given element in the document (for instance the root element) you could try using GetNamespaceOfPrefix.

Categories

Resources