如何将属性添加到现有的xml。帮助我 [英] How to add attribute to existing xml .help me on same

查看:100
本文介绍了如何将属性添加到现有的xml。帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xml

this is my xml

<?xml version="1.0"?>
-<props>
<specific_criteria>NONE</specific_criteria>
<specific_condition>NEW</specific_condition>
-<specific_disposition>
<disposition>NONE</disposition>
</specific_disposition>
<specific_model>NONE</specific_model>
<specific_velocity>NONE</specific_velocity>
<specific_cons_model>NONE</specific_cons_model>
-<specific_program>
<program>NONE</program>
</specific_program>
<specific_operation>NONE</specific_operation>
<specific_oem>NONE</specific_oem>
<specific_esn_status/>
<special_criteria/>
<same_disposition>N</same_disposition>
<same_model>N</same_model>
<same_sku>N</same_sku>
<same_esn_status>N</same_esn_status>
<same_cons_model>N</same_cons_model>
<same_condition>N</same_condition>
<same_velocity>N</same_velocity>
<same_receipt>N</same_receipt>
<same_oem>N</same_oem>
</props>





需要修改如下



and need to modify like below

<props>
  <specific_criteria id=1>NONE</specific_criteria>
  <specific_condition id=2>NEW</specific_condition>
  <specific_disposition>
    <disposition id=3>NONE</disposition>
  </specific_disposition>
  <specific_model id=4>NONE</specific_model>
  <specific_velocity id=5>NONE</specific_velocity>
  <specific_cons_model id=6>NONE</specific_cons_model>
  <specific_program>
    <program id=7>NONE</program>
  </specific_program>
  <specific_operation id=8>NONE</specific_operation>
  <specific_oem id=9>NONE</specific_oem>
  <specific_esn_status id=10/>
  <special_criteria id=11/>
  <same_disposition id=12>N</same_disposition>
  <same_model id=13>N</same_model>
  <same_sku id=14>N</same_sku>
  <same_esn_status id=15>N</same_esn_status>
  <same_cons_model id=16>N</same_cons_model>
  <same_condition id=17>N</same_condition>
  <same_velocity id=18>N</same_velocity>
  <same_receipt id=19>N</same_receipt>
  <same_oem id=20>N</same_oem>
</props>

推荐答案

尝试像这个:

Try something like this:
string scontent = @"<?xml version='1.0'?>
<props>
<specific_criteria>NONE</specific_criteria>
<specific_condition>NEW</specific_condition>
<specific_disposition>
<disposition>NONE</disposition>
</specific_disposition>
<specific_model>NONE</specific_model>
<specific_velocity>NONE</specific_velocity>
<specific_cons_model>NONE</specific_cons_model>
<specific_program>
<program>NONE</program>
</specific_program>
<specific_operation>NONE</specific_operation>
<specific_oem>NONE</specific_oem>
<specific_esn_status/>
<special_criteria/>
<same_disposition>N</same_disposition>
<same_model>N</same_model>
<same_sku>N</same_sku>
<same_esn_status>N</same_esn_status>
<same_cons_model>N</same_cons_model>
<same_condition>N</same_condition>
<same_velocity>N</same_velocity>
<same_receipt>N</same_receipt>
<same_oem>N</same_oem>
</props>"; 

XDocument xdoc = XDocument.Parse(scontent);

var nodes2change =  xdoc.Root.Descendants().Where(x=>x.Descendants().Count() == 0 && (string)x.Value!=string.Empty);
int i = 0;
foreach (XElement x in nodes2change)
{
	i++;
	x.SetAttributeValue("id", i);
}

//xdoc.Save("newFullFileName.xml");







结果:




Result:

<props>
  <specific_criteria id="1">NONE</specific_criteria>
  <specific_condition id="2">NEW</specific_condition>
  <specific_disposition>
    <disposition id="3">NONE</disposition>
  </specific_disposition>
  <specific_model id="4">NONE</specific_model>
  <specific_velocity id="5">NONE</specific_velocity>
  <specific_cons_model id="6">NONE</specific_cons_model>
  <specific_program>
    <program id="7">NONE</program>
  </specific_program>
  <specific_operation id="8">NONE</specific_operation>
  <specific_oem id="9">NONE</specific_oem>
  <specific_esn_status />
  <special_criteria />
  <same_disposition id="10">N</same_disposition>
  <same_model id="11">N</same_model>
  <same_sku id="12">N</same_sku>
  <same_esn_status id="13">N</same_esn_status>
  <same_cons_model id="14">N</same_cons_model>
  <same_condition id="15">N</same_condition>
  <same_velocity id="16">N</same_velocity>
  <same_receipt id="17">N</same_receipt>
  <same_oem id="18">N</same_oem>
</props>





以上代码需要改进。随意改变它!



如需了解更多信息,请参阅: XElement.SetAttributeValue方法(XName,Object)(System.Xml.Linq) [ ^ ]



提示:删除&& (字符串)x.Value!= string.Empty 来自其中语句以获得您期望的结果;)



Above code needs to be improved. Feel free to change it!

For further information, please see: XElement.SetAttributeValue Method (XName, Object) (System.Xml.Linq)[^]

Tip: remove && (string)x.Value!=string.Empty from Where statement to get result as you expect ;)


这篇关于如何将属性添加到现有的xml。帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆