C# - 如何删除 XMLDocument 中具有命名空间的声明和元素?

C# - how to remove declaration and element in XMLDocument that have namespace?

我想删除XML声明信息 标签来自我的 XML 下面的 C# 代码文档。我已经尝试使用示例代码如下所示,但它似乎不起作用,因为我仍然在最终 xml 文档中看到它。

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TimeStamp="2021-05-31T23:01:37.6988918+07:00" Version="16.1" xmlns="http://www.localhost.com/2007/00">
  <Originator CompanyShortName="SITA" />
  <Class>
    <Left>
      <Code CodeContext="3">R12</Code>     
    </Left>
    <Right>
      <Mode RepeatIndex="1" CodeContext="0">ARRIVED</Mode>
      <Status xsi:nil="true" />      
      <Time Qualifier="START" CodeContext="9750" RepeatIndex="1" TimeType="ACT">2021-05-31T21:54:00Z</Time>
      <Time Qualifier="END" CodeContext="0000" RepeatIndex="2" TimeType="">2021-05-31T21:49:00Z</Time>
      <Info>
        <Type CodeContext="3">772</Type>
        <SubType xsi:nil="true" />
        <Reg>T9056</Reg>
        <Tail xsi:nil="true" />
        <Fleet xsi:nil="true" />
      </Info>
    </Right>
  </Class>
</Root>

下面是我试过的代码

  XmlDocument xmlDoc = new XmlDocument();           
   xmlDoc.Load("data.xml");


    XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
    namespaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

    //Remove 
    XmlNodeList nodes = xmlDoc.SelectNodes("//xsi:Info", namespaceManager);
    foreach (XmlNode node in nodes){
        node.ParentNode.RemoveChild(node);                
    }

    //Remove Declaration
    foreach (XmlNode node in xmlDoc) {
        if (node.NodeType == XmlNodeType.XmlDeclaration ) {
            xmlDoc.RemoveChild(node);
        }
    }

这是我最终想要得到的

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TimeStamp="2021-05-31T23:01:37.6988918+07:00" Version="16.1" xmlns="http://www.localhost.com/2007/00">
  <Originator CompanyShortName="SITA" />
  <Class>
    <Left>
      <Code CodeContext="3">R12</Code>     
    </Left>
    <Right>
      <Mode RepeatIndex="1" CodeContext="0">ARRIVED</Mode>
      <Status xsi:nil="true" />      
      <Time Qualifier="START" CodeContext="9750" RepeatIndex="1" TimeType="ACT">2021-05-31T21:54:00Z</Time>
      <Time Qualifier="END" CodeContext="0000" RepeatIndex="2" TimeType="">2021-05-31T21:49:00Z</Time>
    </Right>
  </Class>
</Root>

您介意指导我如何操作以获得所需的预期结果吗?

您需要使用正确的命名空间。 Info 没有任何名称空间前缀,因此我们需要查看文档中此时的默认名称空间是什么。

往回看,我们可以看到 http://www.localhost.com/2007/00 的默认命名空间在 Root 元素上声明:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TimeStamp="2021-05-31T23:01:37.6988918+07:00" Version="16.1" 
      xmlns="http://www.localhost.com/2007/00">

因此,要删除信息节点,我们需要引用 that 命名空间,而不是 XML 模式实例一:

    namespaceManager.AddNamespace("abc", "http://www.localhost.com/2007/00");

    //Remove 
    XmlNodeList nodes = xmlDoc.SelectNodes("//abc:Info", namespaceManager);
    foreach (XmlNode node in nodes)
    {
        node.ParentNode.RemoveChild(node);
    }

请注意,命名空间 prefixes 仅具有“本地”含义。我们 没有 使用 xsi 前缀来引用 http://www.w3.org/2001/XMLSchema-instance 命名空间;同样,我可以使用上面的 abc 来引用 http://www.localhost.com/2007/00 命名空间。