Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Xml中设置值或删除xmlns命名空间_C# - Fatal编程技术网

C# 如何在Xml中设置值或删除xmlns命名空间

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" &

我有一个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" >
  <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")