Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda