Question

No matter what I try I am unable to parse this xml with linq and get the full-path value:

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

</rootfiles>
</container>

and the code:

 var zip = ZipFile.Open(file.FullName, ZipArchiveMode.Read)
 var info = zip.GetEntry("META-INF/container.xml");
            var xml = XElement.Load(info.Open());


            foreach (var b in xml.Elements())
            {
                var xElementt = b.Element("rootfile");

                if (xElementt != null)
                {
                   //xElementt is always null for some reason

                }
           }

All I want is the full-path value

Was it helpful?

Solution

You have a namespace, you need to specify it with your element name:

XNamespace ns = "urn:oasis:names:tc:opendocument:xmlns:container";

var rootFiles = xml.Descendants(ns + "rootfile");

OTHER TIPS

You can use the XDocument.Load static method in order to read the full-path value easily, but don't forget to use the element namespace as a prefix:

var zip = ZipFile.Open(file.FullName, ZipArchiveMode.Read);
var info = zip.GetEntry("META-INF/container.xml");

var doc = XDocument.Load(info.Open());
XNamespace ns = "urn:oasis:names:tc:opendocument:xmlns:container";
var element = doc.Descendants(ns+"rootfile").FirstOrDefault();

if(element != null)
{
    var fullPath = element.Attribute("full-path").Value;
}  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top