C# 如何在Xml中设置值或删除xmlns命名空间
我有一个Xml文件:C# 如何在Xml中设置值或删除xmlns命名空间,c#,C#,我有一个Xml文件: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:inf="http://www.granfol.com/luna" &
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:inf="http://www.granfol.com/luna" >
<Employees>
<Employee Department="e" kk="TEST11111111">
<Name>TestName11111</Name>
</Employee>
</Employees>
</ResourceDictionary>
或
直接删除xmlns默认名称空间
我试过了
取代
X.SetAttributeValue("xmlns","");
上面,
保存时不会抛出异常,
但是当我打开xml文件时,我看到“xmlns”并没有消失
然后我测试并发现
X.Attributes("xmlns")
不包含xmlns,但包含“xmlns:x,xmlns:sys,xmlns:inf”
那么,有什么方法可以在Xml中设置值或删除xmlns名称空间呢
我认为一种方法是再次复制整个xml文件,
但是如果xml文件很大,我不确定这是否容易做到
我真的不知道怎么做,但假设这不是一个好主意。您应该为
http://schemas.microsoft.com/winfx/2006/xaml/presentation
namespace,这样LINQ to XML就可以使用该前缀来限定元素,而不是使用默认名称空间。我不知道为什么,但使用(XmlTextWriter d=newxmltextwriter(@“FILEPATH”,Encoding.UTF8)){d.Formatting=Formatting.Indented;X.WriteTo(d);}而不是X.Save(@“FILEPATH”)
有效。你应该给http://schemas.microsoft.com/winfx/2006/xaml/presentation
namespace,这样LINQ to XML可以使用该前缀而不是使用默认名称空间限定元素。我不确定为什么,但使用(XmlTextWriter d=new XmlTextWriter(@“FILEPATH”,Encoding.UTF8)){d.Formatting=Formatting.Indented;X.WriteTo(d);}
而不是X.Save(@“FILEPATH”);
有效。
X.Attributes("xmlns").Remove();
X.SetAttributeValue("xmlns","");
X.Attributes("xmlns")