Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取XML节点的属性值(使用名称空间)_C#_Xml_Linq To Xml - Fatal编程技术网

C# 获取XML节点的属性值(使用名称空间)

C# 获取XML节点的属性值(使用名称空间),c#,xml,linq-to-xml,C#,Xml,Linq To Xml,这是XML文件 <?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"/>

这是XML文件

<?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>

文本块应更改为“EPUB/wasteland.opf”,但为什么不更改?

您缺少名称空间:

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();
您可以为文本块指定路径:

tbl.Text = path;

您缺少命名空间:

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();
您可以为文本块指定路径:

tbl.Text = path;

您知道您正在循环中分配文本吗?这意味着只有最后一个值可以知道,但只有一个值。它应该是那个,不是吗?只需使用
SingleOrDefault()
FirstOrDefault()
方法获取单个值就可以了哦,非常感谢,我没有注意到名称空间,这个问题花了我一天的时间,你知道你在循环中分配文本吗?这意味着只有最后一个值可以知道,但只有一个值。它应该是那个,不是吗?只需使用
SingleOrDefault()
FirstOrDefault()
方法获取单个值就可以了。哦,它很有效,非常感谢,我没有注意到名称空间,这个问题花了我一天的时间