How to read namespaces c# LINQ to XML - c#

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

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

Extract XML attribute values as variables in C# [duplicate]

I am trying to get Absoluteentry tag's value from the below xml string, but its displaying objectrefrence not set exception
<?xml version="1.0" ?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Body>
<AddResponse xmlns="http://www.sap.com/SBO/DIS">
<PickListParams>
<Absoluteentry>120072</Absoluteentry>
</PickListParams>
</AddResponse>
</env:Body>
</env:Envelope>
Code
XDocument doc = XDocument.Parse(xmlstring);
doc.Element("Envelope").Element("Body").Element("AddResponse").Element("PickListParams").Element("Absoluteentry").Value;
Look at the XML:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
...
That's the Envelope element in the namespace with URI "http://www.w3.org/2003/05/soap-envelope".
Now look at your code:
doc.Element("Envelope")...
That's looking for an Envelope element that's not in any namespace. You should specify the namespace - and the namespaces of the other elements you're looking for:
XNamespace env = "http://www.w3.org/2003/05/soap-envelope";
XNamespace responseNs = "http://www.sap.com/SBO/DIS";
XDocument doc = XDocument.Parse(xmlstring);
var result = doc.Element(env + "Envelope")
.Element(env + "Body")
.Element(responseNs + "AddResponse")
.Element(responseNs + "PickListParams")
.Element(responseNs + "Absoluteentry").Value;
You can use Descendants too. Descendants finds children at any level.
var result = doc.Element(env + "Envelope")
.Element(env + "Body")
.Descendants(responseNs + "Absoluteentry").Value;

How do I get an attribute with namespace?

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

Write reference to xsd/schema in output from XElement.Save()

In C#, assume that I have an XElement (say myXElement) containing some XML structure. By calling
myXElement.Save("/path/to/myOutput.xml");
the XML is written to a text file. However, I would like this textfile to include a reference to a (local) xsd-file (an XML schema). That is, I would like the output to look something like this...
<?xml version="1.0" encoding="utf-8" ?>
<MyElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="MySchema.xsd">
...
How can I do this?
On the root element, just add an attribute:
Example 1:
XmlDocument d = new XmlDocument();
XmlElement e = d.CreateElement("MyElement");
XmlAttribute a = d.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
a.Value = "MySchema.xsd";
d.AppendChild(e);
e.Attributes.Append(a);
Example 2:
XDocument d = new XDocument();
XElement e = new XElement("MyElement");
XAttribute a = new XAttribute(XName.Get("noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance"), "MySchema.xsd");
d.Add(e);
e.Add(a);
You should use XDocument instead of XElement as this contains methods for getting and setting the XML declaratuion etc
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx

Categories

Resources