【发布时间】:2010-10-17 17:03:50
【问题描述】:
我有一个如下的 XML。
<AFConfig>
<Geographies>
<Geography id="Place1" description="NicePlace">
<MetaData>
<Services>
<Service>
...
...
</Service>
</Services>
</MetaData>
<Systems>
<DefaultSystem systemName="SYSONE" server=http"//192.168.0.0" />
</Systems>
</Geography>
<Geographies>
</AFConfig>
我想做的就是这个。
- 克隆元素 Geography 并将其添加为 Sibling(即,Child to Geographies)
- 用新值更新“id”、“description” 和
- 用新值更新系统名称
我的代码。
XDocument xd_Document = XDocument.Load(s_FileName);
XElement xe_Element = (from xe in xd_Document.XPathSelectElements(s_Element)
where xe.Attribute(s_IdAttr).Value == s_Value
select xe).SingleOrDefault();
XElement xe_NewElement = CloneElement(xe_Element)
foreach(KeyValuePair<string, string> s in d_AttrValue)
xe_NewElement.Attribute(s.Key).Value = d_AttrValue[s.Key];
xe_Element.Parent.Add(xe_NewElement);
xd_Document.Save(s_destFileName);
我将以下参数传递给此方法 字符串 s_FileName、字符串 s_destFileName、字符串 s_Element、字符串 s_IdAttr、字符串 s_Value、字典 d_AttrValue
使用此代码,我可以修改 id 和 description 属性。
问题:如何修改 DefaultSystem 属性 systemName 的值?
注意:我有相同的代码来修改现有元素,而无需创建新元素。同样,我遇到了同样的问题。一个通用的解决方案将是首选。
【问题讨论】:
标签: c# xml linq-to-xml