Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda