2012-07-13 47 views

回答

2

您可以使用此扩展方法。它将递归地创建另一个XElement,包括其子名称空间。扩展方法需要放在静态类中:

public static XElement IgnoreNamespace(this XElement xelem) 
    { 
     XNamespace xmlns = ""; 
     var name = xmlns + xelem.Name.LocalName; 
     return new XElement(name, 
         from e in xelem.Elements() 
         select e.IgnoreNamespace(), 
         xelem.Attributes() 
         ); 
    } 


static void Main(string[] args) 
{ 
    var document = XDocument.Parse("<?xml version=\"1.0\" ?><div xmlns=\"http://www.ya.com\"><div class=\"child\"></div></div>"); 
    var first = document.Root.Elements().First().IgnoreNamespace(); 
    Console.WriteLine(first.ToString()); 
} 
+0

感谢:

void Main() { var path = @"C:\Users\User\AppData\Local\Temp\sample.xml"; var text = File.ReadAllText(path); var surrounded = "<test xmlns=\"http://www.ya.com\">" + text + "</test>"; var xe = XElement.Parse(surrounded); xe.Dump(); xe.StripNamespaces().Dump(); var sampleText = "<test xmlns=\"http://www.ya.com\"><div class=\"child\" xmlns:diag=\"http://test.com\"> ** this is some text **</div></test>"; xe = XElement.Parse(sampleText); xe.Dump(); xe.StripNamespaces().Dump(); } public static class Extensions { public static XNode StripNamespaces(this XNode n) { var xe = n as XElement; if(xe == null) return n; var contents = // add in all attributes there were on the original xe.Attributes() // eliminate the default namespace declaration .Where(xa => xa.Name.LocalName != "xmlns") .Cast<object>() // add in all other element children (nodes and elements, not just elements) .Concat(xe.Nodes().Select(node => node.StripNamespaces()).Cast<object>()).ToArray(); var result = new XElement(XNamespace.None + xe.Name.LocalName, contents); return result; } #if !LINQPAD public static T Dump<T>(this T t, string description = null) { if(description != null) Console.WriteLine(description); Console.WriteLine(t); return t; } #endif } 

F#作为第一个写的。我会尝试一下 – user460025 2012-07-13 22:11:02

1

拉曼的答案是好的,但这样它不会对XML的工作:

<div xmlns="http://www.ya.com"> 
    <div class="child"> 
     **This is some text** 
    </div> 
</div> 

这是我的改进版本,将工作:

public static XElement IgnoreNamespace(this XElement xelem) 
{ 
    XNamespace xmlns = ""; 
    var name = xmlns + xelem.Name.LocalName; 

    var result = new XElement(name, 
        from e in xelem.Elements() 
        select e.IgnoreNamespace(), 
        xelem.Attributes() 
        ); 

    if (!xelem.HasElements) 
     result.Value = xelem.Value; 

    return result; 
} 
+0

根据你提供的输入,很难判断这个解决方案是否可用,它抛出'XmlException':'前缀''不能从''重新定义为'http://www.ya。 com''在同一起始元素标记内。' – Maslow 2017-01-19 16:49:50

+0

证明这个解决方案不起作用,因为传递一个没有名字空间的'name',然后复制这些属性(包括名称空间声明)是无效的,因为它应该是。 – Maslow 2017-01-19 19:09:06

0

这里提供的答案都不适用于我。对于在此处登陆的其他人,请使用我自己的大型xaml文件进行测试,并在代码段中包含样本。

let stripNamespaces (e:XElement):XElement= 
    // if the node is not XElement, pass through 
    let rec stripNamespaces (n:XNode): XNode = 
     match n with 
     | :? XElement as x -> 
      let contents = 
       x.Attributes() 
       // strips default namespace, but not other declared namespaces 
       |> Seq.filter(fun xa -> xa.Name.LocalName <> "xmlns") 
       |> Seq.cast<obj> 

       |> List.ofSeq 
       |> (@) (
        x.Nodes() 
        |> Seq.map stripNamespaces 
        |> Seq.cast<obj> 
        |> List.ofSeq 
       ) 
      XElement(XNamespace.None + x.Name.LocalName, contents |> Array.ofList) :> XNode 
     | x -> x 
    stripNamespaces e :?> XElement 
相关问题