【发布时间】:2021-08-14 16:23:04
【问题描述】:
我想从 xml 中删除一些标签。我正在使用 selectNodes() 但我需要传递命名空间。所以我尝试使用 GetXmlNamespaces() 来获取命名空间。一些命名空间没有键值。而且我无法将命名空间作为硬编码值提供,需要定期更改命名空间。我可以传递的唯一值是标签名称和文件。无论如何我可以在c#中获取标签所属的命名空间吗?
例如,我将此命名空间作为硬编码值传递,但我需要以编程方式进行。
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("a", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
XmlNodeList nodes = doc.SelectNodes("//a:workbook/a:fileVersion", nsmgr);
这个命名空间'xmlns="http://schemas.openxmlformats.org/package/2006/relationships"'在我尝试使用获取它时没有密钥
IDictionary<string, string> nameSpaces = GetXmlNamespaces(xmlFilePath);
【问题讨论】:
-
需要查看数据样本。使用 Xml Linq 可以忽略命名空间。