如何修改嵌套的XML中的内容

huangapple go评论58阅读模式
英文:

how to modify contents in a nested xml

问题

需要将CompValue从"C02"修改为"C02,C03"。如何实现?我尝试了各种方法来修改XML内容,但迄今为止未找到解决方案。

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

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>
英文:

I need to modify the CompValue from "C02" to "C02,C03" . How to achieve this ?I have tried various ways to modify the XML content but so far could not find a solution.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(&quot;XML value&quot;);

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

Also

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

This is my sample xml

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

答案1

得分: 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");
    }
}

Output 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>

希望这能帮助你。

英文:

Please try the following solution.

It is using LINQ to XML API that is available in the .Net Framework since 2007.

c#

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

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

Output XML

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

答案2

得分: 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);
英文:

You need to get the complete node and replace the attribute. Here is the example

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

    var critNode = xmlDoc.SelectSingleNode(&quot;//Crit[@CompValue=&#39;C02&#39;]&quot;);

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

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

huangapple
  • 本文由 发表于 2023年7月17日 21:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定