在加载到 XMLDocument 之前,使用 C# 从存储在字符串类型变量中的 XML 中删除完整节点
我已经得到了下面的 XML,它存储在字符串类型变量中,并且我正在使用 .NET 2.0:
<?xml version="1.0"?>
<tcm:ListPublications xmlns:tcm="http://www.tridion.com/ContentManager/5.0" Managed="1">
<tcm:Item ID="tcm:0-437-1" Title="05 Main Australia Web Site (English)"/>
<tcm:Item ID="tcm:0-445-1" Title="06 Internal India Web Site (English)"/>
<tcm:Item ID="tcm:0-437-1" Title="07 EKTA Australia Web Site (English)"/>
<tcm:Item ID="tcm:0-445-1" Title="07 EKTA India Web Site (English)"/>
<tcm:Item ID="tcm:0-414-1" Title="07 Bahrain web Site (Arabic)"/>
<tcm:Item ID="tcm:0-272-1" Title="07 USA web Site (US English)"/>
<tcm:Item ID="tcm:0-279-1" Title="08 Bahrain web Site (English)"/>
<tcm:Item ID="tcm:0-392-1" Title="08 Belgium web Site (French)"/>
<tcm:Item ID="tcm:0-321-1" Title="08 Brazil web Site (English)"/>
</tcm:ListPublications>
现在,在将其加载到我的 XMLDocument 之前,我只想加载那些具有 Title=“07” 的“Item”节点并且标题中不包含“EKTA”。
下面给出了执行此操作的 C# 代码:
//Creating the object of PublicatinBL class.
PublicationBL pubBL = new PublicationBL();
//All publications list XML.
//Here I am getting the whole XML as shown above.
string pubListXML = pubBL.getAllPublicationListXML();
XmlDocument xDocument = new XmlDocument();
//Loading the publication list XML.
//Here before loading the XML, I want to modify it as required above.
xDocument.LoadXml(pubListXML);
I have got the XML below, which is stored in string type variable, and I am using .NET 2.0:
<?xml version="1.0"?>
<tcm:ListPublications xmlns:tcm="http://www.tridion.com/ContentManager/5.0" Managed="1">
<tcm:Item ID="tcm:0-437-1" Title="05 Main Australia Web Site (English)"/>
<tcm:Item ID="tcm:0-445-1" Title="06 Internal India Web Site (English)"/>
<tcm:Item ID="tcm:0-437-1" Title="07 EKTA Australia Web Site (English)"/>
<tcm:Item ID="tcm:0-445-1" Title="07 EKTA India Web Site (English)"/>
<tcm:Item ID="tcm:0-414-1" Title="07 Bahrain web Site (Arabic)"/>
<tcm:Item ID="tcm:0-272-1" Title="07 USA web Site (US English)"/>
<tcm:Item ID="tcm:0-279-1" Title="08 Bahrain web Site (English)"/>
<tcm:Item ID="tcm:0-392-1" Title="08 Belgium web Site (French)"/>
<tcm:Item ID="tcm:0-321-1" Title="08 Brazil web Site (English)"/>
</tcm:ListPublications>
Now, before loading it to my XMLDocument, I want to load only those "Item" nodes which are having Title="07" and does not contain "EKTA" in the title.
And the C# code to do this is given below:
//Creating the object of PublicatinBL class.
PublicationBL pubBL = new PublicationBL();
//All publications list XML.
//Here I am getting the whole XML as shown above.
string pubListXML = pubBL.getAllPublicationListXML();
XmlDocument xDocument = new XmlDocument();
//Loading the publication list XML.
//Here before loading the XML, I want to modify it as required above.
xDocument.LoadXml(pubListXML);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用LINQ to XML:
或者使用LINQ语法:
[更新]之后问题中指定了 .NET 2。
在 .NET 2 中,您可以使用 XPath:
You could use LINQ to XML:
Or with LINQ syntax:
[Update] After .NET 2 was specified in the question.
In .NET 2 you can use XPath: