How do I get an attribute with namespace? - c#

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Element xsi:Attribute="Test"></Element>
</Root>
I'm trying to read the "xsi:Attribute" attribute; the code is like this:
var doc = XDocument.Load(new StringReader(xmlText));
var node = doc.Root.Descendants().First();
XNamespace myNamespace = "xsi";
var attribute = node.Attributes(myNamespace + "Attribute").First();
It throws a "Sequence contains no elements" exception in the last line. What am I doing wrong?

You need to use the actual namespace, not "xsi", which is just a local lookup within the XML file itself for the real namespace:
XNamespace myNamespace = "http://www.w3.org/2001/XMLSchema-instance";

Try this (I believe its more generic):
XNamespace myNamespace = doc.Root.GetNamespaceOfPrefix("xsi");

Related

Access Child nodes with namespace using xpath

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.

How to set namespace on XDocument

I need to acсess xml file. But xml have base namespace whith prefix m:
This is my code, but it do not working, write NullRefference exeptions:
var fileКс = XDocument.Load(somePath);
var allDescrioptions = fileКс.Root.Element("formulas").Elements("formula").ToList();
This is part of xml file:
<?xml version="1.0" encoding="utf-8" ?>
<m:math xmlns:m="http://www.kontur-extern.ru/ФНС 4.0/math.xsd">
<m:formulas>
<m:formula target="#ПрибУб" match="/Файл/Документ/Прибыль/РасчНал" source="Лист 02/стр.060">
</m:formula>
</m:formulas>
</m:math>
I think need to specify namespace, but I don't know how
You can use XNamespace as follow :
XNamespace m = "http://www.kontur-extern.ru/ФНС 4.0/math.xsd";
var fileКс = XDocument.Load(somePath);
var allDescrioptions = fileКс.Root.Element(m+"formulas").Elements(m+"formula").ToList();

Attribute value in root node of xml Linq to XML

While parsing xml by linq to xml, I came across a strange behaviour (atleast to me). Below is the first xml I parsed
`<?xml version="1.0" encoding="UTF-8"?>
<TestRun>
<UnitTestResult testName = "Arun" outcome = "i">
</UnitTestResult>
<UnitTestResult testName = "Arun1" outcome = "i">
</UnitTestResult>
</TestRun>`
My Code looks like
XDocument doc = XDocument.Parse(fileContents);
var result = doc.Descendants("UnitTestResult");
The above works fine. But If my root node contain attributes the same code is not working. What could be the reason. XML sample below
<?xml version="1.0" encoding="UTF-8"?>
<TestRun id="7903b4ff-8706-4379-b9e8-567034b70abb" name="inaambika#INBELW013312A 2016-02-26 16:55:14" runUser="STC\inaambika" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<UnitTestResult testName = "Arun" outcome = "i">
</UnitTestResult>
<UnitTestResult testName = "Arun1" outcome = "i">
</UnitTestResult>
</TestRun>
XDocument doc = XDocument.Parse(fileContents);
var result = doc.Descendants("UnitTestResult");
This one below is not ordinary attribute, it is default namespace declaration :
xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
Having default namespace declared at the root element level, the root element and all descendant elements without prefix (in this case it means all elements in the posted XML) are in that namespace.
You can use XNamespace + element-local-name to reference element in namespace :
XDocument doc = XDocument.Parse(fileContents);
XNamespace d = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
var result = doc.Descendants(d+"UnitTestResult");

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

How to read namespaces c# LINQ to XML

How to read namespaces from code? I want to get xmlns:es, xmlns:un, xmlns:xn attribute values, but I get null attributes, How do I read it?
I try like this:
XNamespace xmlns = xdoc.Root.Attribute("xmlns").Value;
XNamespace ES = xdoc.Root.Attribute(xmlns + "es").Value;
XML:
<?xml version="1.0" encoding="UTF-8"?>
<xmlFile xmlns:es="my.xsd"
xmlns="not_my.xsd">
...
</xmlFile>
XNamespace defns = xdoc.Root.GetDefaultNamespace();
XNamespace es = xdoc.Root.GetNamespaceOfPrefix("es");

Categories

Resources