如何修改嵌套 XML 中的内容

how to modify contents in a nested xml

提问人:Sribin 提问时间:7/17/2023 最后编辑:marc_sSribin 更新时间:7/17/2023 访问量:50

问:

我需要将“C02”修改为“C02,C03”。如何实现这一点?我尝试了各种方法来修改XML内容,但到目前为止还没有找到解决方案。CompValue

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("XML value");

XmlNode root = xmlDoc.DocumentElement;
XmlNode myNode = root.SelectSingleNode("descendant::CompValue");
myNode.Value = "blabla";

root.SelectSingleNode("//CritGroup/Crit").InnerText = "NewValue";

这是我的示例xml

<Search Name="Test1" ProjTypeID="107">
    <SearchType SearchTypeID="20246" />
    <FiscalYear Year="2022" />
    <CorpEnt CEID="367" CEName="Sample" />
    <CritGroup CritGroupID="1" CritGroupAndOr="AND">
        <Crit CritID="205" RelID="275" CompValue="C02" CompValueHuman="C02" AndOr="AND" TextBoxName="tbProjNum" /> 
        <Crit CritID="208" RelID="280" CompValue="11" CompValueHuman="Yes" AndOr="AND" TextBoxName="" />
    </CritGroup>
    <PageSize Size="200" />
</Search>
C# asp.net XML

评论

1赞 GH DevOps 7/17/2023
使用 XDocument 而不是 XmlDocument:stackoverflow.com/questions/16221558/...
0赞 Dai 7/17/2023
@GHDevOps我以为是用于只读XML导航。System.Xml.Linq
0赞 Yitzhak Khabinsky 7/17/2023
@Dai,LINQ to XML API(大约 2007 年以后)是原始 .Net Framework XML API(大约 2002 - 2006 年)的“替代品”。
0赞 Heretic Monkey 7/17/2023
这回答了你的问题吗?如何更改 XML 节点值
0赞 Dai 7/17/2023
@YitzhakKhabinsky 它不是功能等价物:Linq-to-Xml 是围绕替换元素进行更改而不是就地改变它们而设计的 - 例如,你不能覆盖 的子字符串,例如:learn.microsoft.com/en-us/dotnet/standard/linq/...innerText

答:

1赞 Krishna Varma 7/17/2023 #1

您需要获取完整的节点并替换属性。下面是示例

    var xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);

    var critNode = xmlDoc.SelectSingleNode("//Crit[@CompValue='C02']");

    if (critNode != null)
    {
        var compValueAttr = critNode.Attributes["CompValue"];
        if (compValueAttr != null)
        {
            compValueAttr.Value = "C02,C03";
        }
    }

    var modifiedXml = xmlDoc.OuterXml;
    Console.WriteLine(modifiedXml);

评论

0赞 Sribin 7/17/2023
CompValue 的值并不总是“C02”
2赞 Yitzhak Khabinsky 7/17/2023 #2

请尝试以下解决方案。

它使用自 2007 年以来在 .Net Framework 中提供的 LINQ to XML API。

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<Search Name='Test1' ProjTypeID='107'>
            <SearchType SearchTypeID='20246'/>
            <FiscalYear Year='2022'/>
            <CorpEnt CEID='367' CEName='Sample'/>
            <CritGroup CritGroupID='1' CritGroupAndOr='AND'>
                <Crit CritID='205' RelID='275' CompValue='C02' CompValueHuman='C02'
                      AndOr='AND' TextBoxName='tbProjNum'/>
                <Crit CritID='208' RelID='280' CompValue='11' CompValueHuman='Yes'
                      AndOr='AND' TextBoxName=''/>
            </CritGroup>
            <PageSize Size='200'/>
        </Search>");

    var Crits = xdoc.Descendants("Crit")
        .Where(x => x.Attribute("CompValue").Value == "C02");
    
    foreach (XElement Crit in Crits)
    {
        Crit.Attribute("CompValue").SetValue("C02,C03");
    }
}

输出 XML

<Search Name="Test1" ProjTypeID="107">
  <SearchType SearchTypeID="20246" />
  <FiscalYear Year="2022" />
  <CorpEnt CEID="367" CEName="Sample" />
  <CritGroup CritGroupID="1" CritGroupAndOr="AND">
    <Crit CritID="205" RelID="275" CompValue="C02,C03" CompValueHuman="C02" AndOr="AND" TextBoxName="tbProjNum" />
    <Crit CritID="208" RelID="280" CompValue="11" CompValueHuman="Yes" AndOr="AND" TextBoxName="" />
  </CritGroup>
  <PageSize Size="200" />
</Search>