Xml 命名空间前缀规范器

Xml Namespace Prefix Normalizer

我有 XML 看起来像这样:

<ns1:TextXML xmlns:ns1="http://www.example.com/namespace/1" xmlns:ns2="http://www.example.com/namespace/2" xmlns:ns3="http://www.example.com/namespace/3">
    <n2:Child1 xmlns:n2="http://www.example.com/namespace/2">
        <n3:Child2 xmlns:n3="http://www.example.com/namespace/3">THIS IS CONTENT</n3:Child2>
    </n2:Child1>
</ns1:TextXML>

以下命名空间前缀映射到相同的带下划线的命名空间 URI

ns2 和 n2 => http://www.example.com/namespace/2

ns3 和 n3 => http://www.example.com/namespace/3

使用 C#,我想转换 XML 以便命名空间前缀全部标准化并像这样移动到根元素

<ns1:TextXML xmlns:ns1="http://www.example.com/namespace/1" xmlns:ns2="http://www.example.com/namespace/2" xmlns:ns3="http://www.example.com/namespace/3">
    <ns2:Child1>
        <ns3:Child2>THIS IS CONTENT</ns3:Child2>
    </ns2:Child1>
</ns1:TextXML>

我知道这段代码会将所有命名空间声明移动到 XML:

的顶部
string strNewXML = @"<ns1:TextXML xmlns:ns1=""http://www.example.com/namespace/1"" xmlns:ns2=""http://www.example.com/namespace/2"" xmlns:ns3=""http://www.example.com/namespace/3"">
<ns2:Child1 xmlns:ns2=""http://www.example.com/namespace/2"">
<ns3:Child2 xmlns:ns3=""http://www.example.com/namespace/3"">THIS IS CONTENT</ns3:Child2>
</ns2:Child1>
</ns1:TextXML>";

var docNewXML = new XmlDocument();
docNewXML.LoadXml(strNewXML);
var docNewXMLns = new XmlNamespaceManager(docNewXML.NameTable);
docNewXMLns.AddNamespace("ns1", "http://www.example.com/namespace/1");
docNewXMLns.AddNamespace("ns2", "http://www.example.com/namespace/2");
docNewXMLns.AddNamespace("ns3", "http://www.example.com/namespace/3");

XmlWriterSettings xwsCfg = new XmlWriterSettings();
xwsCfg.Indent = false;
xwsCfg.NamespaceHandling = NamespaceHandling.OmitDuplicates; // This is doing the heavy lifting
xwsCfg.NewLineOnAttributes = false;
xwsCfg.OmitXmlDeclaration = true;
xwsCfg.Encoding = Encoding.UTF8;

StringWriter swOutput = new EnvelopeFactory.EncodableStringWriter();
XmlWriter xwOut = XmlWriter.Create(swOutput, xwsCfg);
docNewXML.WriteTo(xwOut);
xwOut.Flush();
xwOut.Close();

var result = swOutput.ToString();

结果=

<ns1:TextXML xmlns:ns1="http://www.example.com/namespace/1" xmlns:ns2="http://www.example.com/namespace/2" xmlns:ns3="http://www.example.com/namespace/3">
    <ns2:Child1>
        <ns3:Child2>THIS IS CONTENT</ns3:Child2>
    </ns2:Child1>
</ns1:TextXML>

然而,当我将 strNewXML 更改为:

<ns1:TextXML xmlns:ns1="http://www.example.com/namespace/1" xmlns:ns2="http://www.example.com/namespace/2" xmlns:ns3="http://www.example.com/namespace/3">
    <n2:Child1 xmlns:n2="http://www.example.com/namespace/2">
        <n3:Child2 xmlns:n3="http://www.example.com/namespace/3">THIS IS CONTENT</n3:Child2>
    </n2:Child1>
</ns1:TextXML>

那么结果=

<ns1:TextXML xmlns:ns1="http://www.example.com/namespace/1" xmlns:ns2="http://www.example.com/namespace/2" xmlns:ns3="http://www.example.com/namespace/3">
    <n2:Child1 xmlns:n2="http://www.example.com/namespace/2">
        <n3:Child2 xmlns:n3="http://www.example.com/namespace/3">THIS IS CONTENT</n3:Child2>
    </n2:Child1>
</ns1:TextXML>

我仍然有重复的命名空间

有人知道完成我想做的事情的好方法吗?

使用 XDocument 和 LINQ to XML,您可以调整内部元素的属性以删除重复的命名空间声明,并自动使用正确的命名空间。

var xdoc = XDocument.Parse(strNewXML);

var nsDecls = xdoc.Descendants()
                  .Attributes()
                  .Where(a => a.IsNamespaceDeclaration)
                  .GroupBy(a => a.Value)
                  .Select(ag => ag.DistinctBy(a => a.Name));
var wantedNSDecls = nsDecls.Select(ag => ag.First()).ToHashSet();

foreach (var xe in xdoc.Root.DescendantsAndSelf()) {
    var keepAttribs = xe.Attributes().Where(a => !a.IsNamespaceDeclaration || wantedNSDecls.Contains(a));
    xe.ReplaceAttributes(keepAttribs);
}

PS 我的回答使用了扩展方法,DistinctBy。您也可以使用 GroupBy().Select(.First()) 代替它。这是扩展方法:

public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> keyFn, IEqualityComparer<TKey> comparer = null) {
    var seenKeys = new HashSet<TKey>(comparer);
    foreach (var item in items)
        if (seenKeys.Add(keyFn(item)))
            yield return item;
}

对于 .Net FW < 4.7.2 或 .Net Core < 2,您将需要 ToHashSet 扩展方法:

public static HashSet<T> ToHashSet<T>(this IEnumerable<T> items) => new HashSet<T>(items); // including in .Net 4.7.2, .Net Core >2
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> items, IEqualityComparer<T> cmp) => new HashSet<T>(items, cmp);