[英]Parsing xml file with linq
无论我尝试什么,我都无法使用linq解析此xml并获取完整路径值:
<?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>
和代码:
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
}
}
我想要的只是全路径值
您有一个名称空间,需要使用您的元素名称指定它:
XNamespace ns = "urn:oasis:names:tc:opendocument:xmlns:container";
var rootFiles = xml.Descendants(ns + "rootfile");
您可以使用XDocument.Load
静态方法来轻松读取full-path
值,但不要忘记使用element
名称空间作为前缀:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.