gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:05:23 24 4
gpt4 key购买 nike

我想删除 XML 声明 信息 在 C# 代码中从我的 XML 文档中标记下面的代码。我已经尝试使用如下所示的示例代码,但它似乎不起作用,因为我仍然在最终的 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/00Root 上声明元素:

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

因此,要删除信息节点,我们需要引用 那个命名空间,而不是 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);
}
请注意,命名空间前缀仅具有“本地”含义。我们不必使用 xsi前缀引用 http://www.w3.org/2001/XMLSchema-instance命名空间;同样,我可以使用 abc以上引用 http://www.localhost.com/2007/00命名空间。

关于C# - 如何删除具有命名空间的 XMLDocument 中的声明和元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68283558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com