I try to get informations from an xml file and so far I am quite good with the navigation via
doc.Element("foo1").Element("foo2").Value
But now I am running into a wall and I think it is because of the namespace.
I use this code snippet to filter a specific part out of my xml doc:
XNamespace xmlns = "http://www.siemens.com/automation/Openness/SW/Interface/v4";
IEnumerable<XElement> de =
from el in doc.Descendants(xmlns + "Section")
where el.Attribute("Name").Value == "Input"
select el;
The namespace is given and the resulting XElement is the followed one:
<Section Name="Input" xmlns="http://www.siemens.com/automation/Openness/SW/Interface/v4">
<Member Name="STARTTASTER" Datatype="Bool" Remanence="NonRetain" Accessibility="Public">
<AttributeList>
<BooleanAttribute Name="ExternalAccessible" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="ExternalVisible" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="ExternalWritable" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="UserVisible" Informative="true" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="UserReadOnly" Informative="true" SystemDefined="true">false</BooleanAttribute>
<BooleanAttribute Name="UserDeletable" Informative="true" SystemDefined="true">true</BooleanAttribute>
</AttributeList>
</Member>
</Section>
That is exactly what I want to have and I can save it at root
Now I use
var foo = root.Element(xmlns + "Section");
Console.WriteLine(foo);
And I would expect to get this
<Member Name="STARTTASTER" Datatype="Bool" Remanence="NonRetain" Accessibility="Public">
<AttributeList>
<BooleanAttribute Name="ExternalAccessible" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="ExternalVisible" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="ExternalWritable" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="UserVisible" Informative="true" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="UserReadOnly" Informative="true" SystemDefined="true">false</BooleanAttribute>
<BooleanAttribute Name="UserDeletable" Informative="true" SystemDefined="true">true</BooleanAttribute>
</AttributeList>
</Member>
But I just get nothing. Now my first question would be why and secondly, how could I achieve my expected outcome.
I would be also happy if there is a nicer/more practical way. I actually want to store the value "STARTTASTER" into a variable.
Please try the following solution.
I saved your XML as a e:\Temp\Aschenauer.xml file:
<Section Name="Input" xmlns="http://www.siemens.com/automation/Openness/SW/Interface/v4">
<Member Name="STARTTASTER" Datatype="Bool" Remanence="NonRetain" Accessibility="Public">
<AttributeList>
<BooleanAttribute Name="ExternalAccessible" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="ExternalVisible" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="ExternalWritable" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="UserVisible" Informative="true" SystemDefined="true">true</BooleanAttribute>
<BooleanAttribute Name="UserReadOnly" Informative="true" SystemDefined="true">false</BooleanAttribute>
<BooleanAttribute Name="UserDeletable" Informative="true" SystemDefined="true">true</BooleanAttribute>
</AttributeList>
</Member>
</Section>
c#
void Main()
{
const string filename = @"e:\Temp\Aschenauer.xml";
XDocument xdoc = XDocument.Load(filename);
XNamespace ns = xdoc.Root.GetDefaultNamespace();
var NameAttr = xdoc.Descendants(ns + "Member")
.Attributes("Name").FirstOrDefault().Value;
Console.WriteLine($"Name='{NameAttr}'");
}
Output
Name='STARTTASTER'
To answer the specific 'why do I get nothing?' part, in your code here:
root.Element(xmlns + "Section")
root is a Section element you obtained from the initial query. The code above is searching for the first child element called Section. There are none, so you get null. What you seem to be looking for is the first child element called Member:
root.Element(xmlns + "Member")