简体   繁体   English

在C#中删除XML的一部分

[英]Delete Part Of XML in C#

I have a XML file in my Visual Web Developer project that looks like this: 我的Visual Web Developer项目中有一个XML文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<complaints>
    <complaint>
        <user>omern</user>
        <content>asd</content>
        <ID>1</ID>
    </complaint>
    <complaint>
        <user>omeromern</user>
        <content>try2</content>
        <ID>2</ID>
    </complaint>
</complaints>    

I want to delete complaint nodes that have and ID of 2. How can I do this? 我要删除ID为2的complaint节点。我该怎么做?

You can use the System.Xml.XmlDocument class to modify XML documents in C#. 您可以使用System.Xml.XmlDocument类来修改C#中的XML文档。 Note that this class lives in the System.Xml.dll assembly, so you will need to add a reference to System.Xml in your project. 请注意,该类位于System.Xml.dll程序集中,因此您需要在项目中添加对System.Xml的引用。

using System.Xml;
internal class XmlExample
{
    /// <summary>
    /// Takes an XML string and removes complaint nodes with an ID of 2.
    /// </summary>
    /// <param name="xml">An XML document in string form.</param>
    /// <returns>The XML document with nodes removed.</returns>
    public static string StripComplaints(string xml)
    {
        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(xml);
        XmlNodeList nodes = xdoc.SelectNodes("/complaints/complaint[ID = '2']");
        XmlNode complaintsNode = xdoc.SelectSingleNode("/complaints");
        foreach (XmlNode n in nodes)
        {
            complaintsNode.RemoveChild(n);
        }

        return xdoc.OuterXml;
    }
}

Usage: 用法:

string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
                <complaints>
                    <complaint>
                        <user>omern</user>
                        <content>asd</content>
                        <ID>1</ID>
                    </complaint>
                    <complaint>
                        <user>omeromern</user>
                        <content>try2</content>
                        <ID>2</ID>
                    </complaint>
                </complaints>";
xml = XmlExample.StripComplaints(xml);
//using System.Xml;

public string RemoveComplaintWhereIDis(string xml, string id)
{
    XmlDocument x = new XmlDocument();
    xml.LoadXml(xml);
    foreach (XmlNode xn in x.LastChild.ChildNodes)
    {
        if (xn.LastChild.InnerText == id)
        {
            x.LastChild.RemoveChild(xn);
        }
    }
    return x.OuterXml;
}

Basic Usage: 基本用法:

string x = @"<?xml version=""1.0"" encoding=""utf-8""?>
             <complaints>
                 <complaint>
                     <user>omern</user>
                     <content>asd</content>
                     <ID>1</ID>
                 </complaint>
                 <complaint>
                     <user>omeromern</user>
                     <content>try2</content>
                     <ID>2</ID>
                 </complaint>
             </complaints>";

string without2 = RemoveComplaintWhereIDis(x, "2");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM