【发布时间】:2015-09-11 10:33:17
【问题描述】:
我正在尝试替换我的 XML 文件中的一个节点,但我需要一些帮助来解决如何用非唯一名称或标识符替换节点。
问题是,如何用 XmlDataReplace 中的数据替换以下节点:
/Document/ExclusionContainer/Mobile_Devices
以下是示例代码中使用的一些示例代码和数据。
示例代码
private void ReplaceXmlArray()
{
System.Xml.XmlDocument OriginalXmlDoc = new System.Xml.XmlDocument();
//Scroll Down for XmlData
OriginalXmlDoc.LoadXml("StackOverflow - XmlDataOriginal");
string NewXmlContent = "StackOverFlow - XmlDataReplace";
//Need help here on Code to Replace
//Node in OriginalXmlDoc
}
XmlDataOriginal - 原始文档
<Document>
<DefaultContainer>
<Mobile_Devices>
<Mobile_Device>
<Id>1</Id>
<Name>Device-One</Name>
</Mobile_Device>
</Mobile_Devices>
</DefaultContainer>
<ExlusionContainer>
<Mobile_Devices>
<Mobile_Device>
<Id>2</Id>
<Name>Device-Two</Name>
</Mobile_Device>
</Mobile_Devices>
<Laptops />
</ExclusionContainer>
</Document>
XmlDataReplace - 将替换 XmlData1 (Document/ExclusionContainer/) 中的数据
<Mobile_Devices>
<Mobile_Device>
<Id>2</Id>
<Name>Device-Two</Name>
</Mobile_Device>
<Mobile_Device>
<Id>3</Id>
<Name>Device-Three</Name>
</Mobile_Device>
</Mobile_Devices>
【问题讨论】:
-
说实话,您的要求非常不清楚 - 您甚至没有显示任何代码来 try 进行替换,也不清楚以何种方式@ 987654325@标识多个节点...
-
问题是“如何将 /Document/ExclusionContainer/Mobile_Devices 替换为 'XmlDataReplace' 中的数据”。我尝试这样做,但在收集正确的节点时遇到问题
-
那么在寻找合适的节点方面,您尝试过什么?更换部件目前似乎无关紧要 - 你的问题实际上是关于找到它。就我个人而言,我强烈建议使用 LINQ to XML 而不是 XmlDocument - 这将比使用 XmlDocument 更容易。
-
是的,我同意这可能更多是为了找到它。到目前为止,OriginalXmlDoc.GetElementsByTagName 方法一直给我例外,但我会研究 Linq to Xml。谢谢