一个人怎么可以在文档中找到未知的XML命名空间?文档、中找到、个人、空间

2023-09-04 02:32:01 作者:铥了情丶

具体(深吸一口气):你会如何去在C#/找到所有的XML命名空间NET 的XmlDocument 对其中有中没有适用的模式实例的的XmlSchemaSet 架构属性)?

Specifically (taking a deep breath): How would you go about finding all the XML name spaces within a C#/.NET XmlDocument for which there are no applicable schemas in the instance's XmlSchemaSet (Schemas property)?

我的XPath神奇的是缺乏成熟到做这样的事情,但是我将继续寻找在此期间...

My XPath magic is lacking the sophistication to do something like this, but I will keep looking in the meantime ...

推荐答案

您需要把所有的不同的命名空间的文档中的列表,然后比较,与不同的命名空间的架构设置。

You need to get a list of all the distinct namespaces in the document, and then compare that with the distinct namespaces in the schema set.

但空间声明的名称通常不中的XPath文档模型曝光。但考虑到一个节点,你可以得到它的命名空间:

But namespace declaration names are typically not exposed in the XPath document model. But given a node you can get its namespace:

// Match every element and attribute in the document
var allNodes = xmlDoc.SelectNodes("//(*|@*)");
var found = new Dictionary<String, bool>(); // Want a Set<string> really
foreach (XmlNode n in allNodes) {
  found[n.NamespaceURI] = true;
}
var allNamespaces = found.Keys.OrderBy(s => s);