在加载到 XMLDocument 之前,使用 C# 从存储在字符串类型变量中的 XML 中删除完整节点

发布于 2024-11-11 09:25:05 字数 1482 浏览 1 评论 0原文

我已经得到了下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-11-18 09:25:05

您可以使用LINQ to XML

var xdocument = XDocument.Parse(xml);

var nodes = xdocument.Descendants(XName.Get("Item", "http://www.tridion.com/ContentManager/5.0"))
    .Where(arg => arg.Attribute("Title").Value.Contains("07") && !arg.Attribute("Title").Value.Contains("EKTA"))
    .ToList();

或者使用LINQ语法:

var nodes = (
    from node in xdocument.Descendants(XName.Get("Item", "http://www.tridion.com/ContentManager/5.0"))
    let titleAttribute = node.Attribute("Title").Value
    where titleAttribute.Contains("07") && !titleAttribute.Contains("EKTA")
    select node)
    .ToList();

[更新]之后问题中指定了 .NET 2。

在 .NET 2 中,您可以使用 XPath:

XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDocument.NameTable);
xmlnsManager.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");
XmlNodeList nodes = xmlDocument.SelectNodes(
    "//tcm:ListPublications//tcm:Item[contains(@Title,'07') and not(contains(@Title,'EKTA'))]",
    xmlnsManager);

You could use LINQ to XML:

var xdocument = XDocument.Parse(xml);

var nodes = xdocument.Descendants(XName.Get("Item", "http://www.tridion.com/ContentManager/5.0"))
    .Where(arg => arg.Attribute("Title").Value.Contains("07") && !arg.Attribute("Title").Value.Contains("EKTA"))
    .ToList();

Or with LINQ syntax:

var nodes = (
    from node in xdocument.Descendants(XName.Get("Item", "http://www.tridion.com/ContentManager/5.0"))
    let titleAttribute = node.Attribute("Title").Value
    where titleAttribute.Contains("07") && !titleAttribute.Contains("EKTA")
    select node)
    .ToList();

[Update] After .NET 2 was specified in the question.

In .NET 2 you can use XPath:

XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDocument.NameTable);
xmlnsManager.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");
XmlNodeList nodes = xmlDocument.SelectNodes(
    "//tcm:ListPublications//tcm:Item[contains(@Title,'07') and not(contains(@Title,'EKTA'))]",
    xmlnsManager);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文