解析LINQ的XML文件 [英] Parsing xml file with linq

查看:113
本文介绍了解析LINQ的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不管我怎么努力,我无法分析该XML的LINQ,并得到全路径值:

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>

和代码:

 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

                }
           }

我要的是全路径值

推荐答案

您有一个命名空间,你需要用你的元素名称指定它:

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");

这篇关于解析LINQ的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆