0

How can I get the innertext of a xml node based on his attribute (type string)?

My XML-File looks following.

<?xml version="1.0" encoding="UTF-8" ?>
<office:document-content office:version="1.2">
  <office:body>
    <office:text>
      <table:table table:name="Tabelle6" table:style-name="Tabelle6">
        <table:table-row>
          <table:table-cell table:style-name="Tabelle6.A1" office:value-type="string">
            <text:p text:name="Invoice" text:style-name="P6">0001</text:p>
          </table:table-cell>
        </table:table-row>
      </table:table>
    </office:text>
  </office:body>
</office:document-content>

I want to get the invoice number (0001) from this xml file. My code looks like this

var xml = XDocument.Load(filePath);

var query = from item in xml.Elements("text:p")
            where (string)item.Attribute("text:name").Value == "Invoice"
            select item.Value;

If I execute this, I get an error:

The ': ' character, hexadecimal value 0x3A, must not be contained in a name.

Maybe it's important, the content.xml is a part of a extracted .odt-File.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
MyNewName
  • 1,035
  • 2
  • 18
  • 34
  • 3
    `text` is not part of the element/attribute name, it's the namespace of that attribute. Try `xml.Elements(XName.Get("text","whatever the namespace url is"))` instead. – Manfred Radlwimmer Jan 19 '17 at 09:30
  • A detailed Explanation can be found [here on MSDN](https://msdn.microsoft.com/en-us/library/bb299741(v=vs.110).aspx) (The code above is just an example, not the actual solution) – Manfred Radlwimmer Jan 19 '17 at 09:42
  • @ManfredRadlwimmer thanks. It's the first time I'm working with linq. How can I display the invoice number? – MyNewName Jan 19 '17 at 09:42
  • For that you can [read this question/answer](http://stackoverflow.com/questions/9185226/best-way-to-query-xdocument-with-linq) – Manfred Radlwimmer Jan 19 '17 at 09:43

0 Answers0