1

Here's the XML file

<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile media-type="application/oebps-package+xml" full-path="EPUB/wasteland.opf"/>
</rootfiles>
</container>

And here's my code

      XElement XmlElement;
      XmlElement =  XElement.Load(containerXml.Stream);


      TextBlock tbl= new TextBlock();
      foreach (XElement level1Element in XmlElement.Elements("rootfiles"))
      {
          foreach (XElement level2Element in level1Element.Elements("rootfile"))
          {
              tbl.Text = level2Element.Attribute("full-path").Value;
          }
      }

The textblock should change to "EPUB/wasteland.opf" but why it wouldn't?

4
  • You know that you are assigning text in a loop? That means only last value will stay Commented Jun 27, 2013 at 22:04
  • I know, but there's only one value. It should be that one, isn't it? Commented Jun 27, 2013 at 22:06
  • Simply get single value with FirstOrDefault() of SingleOrDefault() method Commented Jun 27, 2013 at 22:11
  • 1
    OH It worked, thank you very much, i didn't notice the Namespace, this problem took me a day so awful Commented Jun 27, 2013 at 22:15

1 Answer 1

1

You are missing namespace:

var xdoc = XDocument.Load(containerXml.Stream);
XNamespace ns = "urn:oasis:names:tc:opendocument:xmlns:container";
var path = xdoc.Descendants(ns + "rootfile")
                .Select(r => (string)r.Attribute("full-path"))
                .FirstOrDefault();

And you can assign path to text block:

tbl.Text = path;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.