C# 在C中从StringBuilder中删除XML节点#

C# 在C中从StringBuilder中删除XML节点#,c#,asp.net,xml,stringbuilder,C#,Asp.net,Xml,Stringbuilder,我在StringBuilder中有结构节点,现在我想删除一些包含某一年的节点,例如,我想删除所有具有此结构的节点: 及其内部的所有节点 我的xml中的代码: <dropDownValue caption='2013' key='2013'> <dropDown default='sdrive23i.aspx#ulTopMenu'> <description caption='Модификация'> <modif

我在
StringBuilder
中有结构节点,现在我想删除一些包含某一年的节点,例如,我想删除所有具有此结构的节点:

及其内部的所有节点

我的xml中的代码:

<dropDownValue caption='2013' key='2013'>
    <dropDown default='sdrive23i.aspx#ulTopMenu'>
        <description caption='Модификация'>
        <modifyResource placeholder='modification'/>
        </description>
        <dropDownValue caption='sDrive23i' key='sdrive23i.aspx#ulTopMenu'/>
        <dropDownValue caption='sDrive30i' key='sdrive30i.aspx#ulTopMenu'/>
        <dropDownValue caption='sDrive35i' key='sdrive35i.aspx#ulTopMenu'/>
    </dropDown>
</dropDownValue>


<dropDownValue caption='2013' key='2013'>
    <dropDown default='30d.aspx#ulTopMenu'>
        <description caption='Модификация'>
            <modifyResource placeholder='modification'/>
        </description>
        <dropDownValue caption='3.0D' key='30d.aspx#ulTopMenu'/>
        <dropDownValue caption='3.6' key='36.aspx#ulTopMenu'/>
        <dropDownValue caption='4.8GTS' key='48gts.aspx#ulTopMenu'/>
        <dropDownValue caption='4.8S' key='48s.aspx#ulTopMenu'/>
        <dropDownValue caption='4.8Turbo S' key='48turbos.aspx#ulTopMenu'/>
    </dropDown>
</dropDownValue>


<dropDownValue caption='2013' key='2013'>
    <dropDown default='27.aspx#ulTopMenu'>
        <description caption='Модификация'>
            <modifyResource placeholder='modification'/>
        </description>
        <dropDownValue caption='2.7' key='27.aspx#ulTopMenu'/>
        <dropDownValue caption='S 3.4' key='s34.aspx#ulTopMenu'/>
    </dropDown>
</dropDownValue>

<dropDownValue caption='2013' key='2013'>
    <dropDown default='4s_4_8.aspx#ulTopMenu'>
        <description caption='Модификация'>
            <modifyResource placeholder='modification'/>
        </description>
        <dropDownValue caption='4S 4.8' key='4s_4_8.aspx#ulTopMenu'/>
        <dropDownValue caption='S 4.8' key='s_4_8.aspx#ulTopMenu'/>
        <dropDownValue caption='Turbo 4.8' key='turbo_4_8.aspx#ulTopMenu'/>
    </dropDown>
</dropDownValue>

使用XDocument如何:

var xml = XDocument.Load("yourfile.xml");

xml.Descendants("dropDownValue")
   .Where(e => e.Attribute("caption").Value == "2013" && e.Attribute("key").Value == "2013").Remove();

//your xml as a string again
var stringXml = xml.ToString();
或者使用XmlDocument:

var xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");

XmlNodeList nodes = xmlDoc.SelectNodes("//dropDownValue[@caption='2013' and @key='2013']");

foreach (XmlNode node in nodes)
    node.ParentNode.RemoveChild(node);

//your xml as a string again
var xmlDocString = xmlDoc.OuterXml;

使用XDocument如何:

var xml = XDocument.Load("yourfile.xml");

xml.Descendants("dropDownValue")
   .Where(e => e.Attribute("caption").Value == "2013" && e.Attribute("key").Value == "2013").Remove();

//your xml as a string again
var stringXml = xml.ToString();
或者使用XmlDocument:

var xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");

XmlNodeList nodes = xmlDoc.SelectNodes("//dropDownValue[@caption='2013' and @key='2013']");

foreach (XmlNode node in nodes)
    node.ParentNode.RemoveChild(node);

//your xml as a string again
var xmlDocString = xmlDoc.OuterXml;

使用XDocument如何:

var xml = XDocument.Load("yourfile.xml");

xml.Descendants("dropDownValue")
   .Where(e => e.Attribute("caption").Value == "2013" && e.Attribute("key").Value == "2013").Remove();

//your xml as a string again
var stringXml = xml.ToString();
或者使用XmlDocument:

var xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");

XmlNodeList nodes = xmlDoc.SelectNodes("//dropDownValue[@caption='2013' and @key='2013']");

foreach (XmlNode node in nodes)
    node.ParentNode.RemoveChild(node);

//your xml as a string again
var xmlDocString = xmlDoc.OuterXml;

使用XDocument如何:

var xml = XDocument.Load("yourfile.xml");

xml.Descendants("dropDownValue")
   .Where(e => e.Attribute("caption").Value == "2013" && e.Attribute("key").Value == "2013").Remove();

//your xml as a string again
var stringXml = xml.ToString();
或者使用XmlDocument:

var xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");

XmlNodeList nodes = xmlDoc.SelectNodes("//dropDownValue[@caption='2013' and @key='2013']");

foreach (XmlNode node in nodes)
    node.ParentNode.RemoveChild(node);

//your xml as a string again
var xmlDocString = xmlDoc.OuterXml;


为什么要使用StringBuilder?只要使用一个XML API…@JonSkeet我在XML文档中插入我的stringBuilder,如何删除XmlDocument中的节点?XmlDocument doc=新的XmlDocument();doc.LoadXml(strBuilder.ToString());现在还不清楚你为什么会有一个StringBuilder。但是我不会使用XmlDocument,我会使用linqtoxml,为什么要使用StringBuilder呢?只要使用一个XML API…@JonSkeet我在XML文档中插入我的stringBuilder,如何删除XmlDocument中的节点?XmlDocument doc=新的XmlDocument();doc.LoadXml(strBuilder.ToString());现在还不清楚你为什么会有一个StringBuilder。但是我不会使用XmlDocument,我会使用linqtoxml,为什么要使用StringBuilder呢?只要使用一个XML API…@JonSkeet我在XML文档中插入我的stringBuilder,如何删除XmlDocument中的节点?XmlDocument doc=新的XmlDocument();doc.LoadXml(strBuilder.ToString());现在还不清楚你为什么会有一个StringBuilder。但是我不会使用XmlDocument,我会使用linqtoxml,为什么要使用StringBuilder呢?只要使用一个XML API…@JonSkeet我在XML文档中插入我的stringBuilder,如何删除XmlDocument中的节点?XmlDocument doc=新的XmlDocument();doc.LoadXml(strBuilder.ToString());现在还不清楚你为什么会有一个StringBuilder。但我不会使用XmlDocument-我会使用LINQ to XML.Error 50'System.Collections.Generic.IEnumerable'不包含'Remove'的定义,并且找不到接受'System.Collections.Generic.IEnumerable'类型的第一个参数的扩展方法'Remove'。好的,如果您想使用XmlDocument.Error 50'System.Collections.Generic.IEnumerable'不包含'Remove'的定义,并且找不到接受'System.Collections.Generic.IEnumerable'类型的第一个参数的扩展方法'Remove'(是否缺少using指令或程序集引用?),好的,如果您想使用XmlDocument.Error 50'System.Collections.Generic.IEnumerable'不包含'Remove'的定义,并且找不到接受'System.Collections.Generic.IEnumerable'类型的第一个参数的扩展方法'Remove'(是否缺少using指令或程序集引用?),好的,如果您想使用XmlDocument.Error 50'System.Collections.Generic.IEnumerable'不包含'Remove'的定义,并且找不到接受'System.Collections.Generic.IEnumerable'类型的第一个参数的扩展方法'Remove'(是否缺少using指令或程序集引用?),好的,如果要使用XmlDocument,我添加了一个方法。