我的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>我知道这段代码将把所有的Namespace声明移到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>我仍然只剩下重复的命名空间。
有没有人知道有什么好方法可以完成我正在尝试做的事情?
发布于 2020-11-19 02:51:52
使用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核心< 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);https://stackoverflow.com/questions/64866423
复制相似问题