Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
Understand xml namespaces used with linq

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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'

over 4 years ago · Santiago Trujillo Report

0

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")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!