读取 XML 模板,进行更改并显示在页面上 [英] Reading XML template, make a change and display on page
本文介绍了读取 XML 模板,进行更改并显示在页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有一个预先格式化的 xml 模板文件,其中列出了所有国家/地区代码.
Hi I have a preformatted xml template file that lists all country codes.
我需要将其加载到 C# scipt 中并循环遍历并将 mc_name 属性与国家/地区代码匹配,并向节点添加值属性.
I need to load this into a C# scipt and loop through and match the mc_name attribute to a country code and add a value attribute to the node.
然后,我需要在页面上呈现编辑过的 xml,以便将其用作 Flash 图表的数据源.
I then need to render the edited xml on the page so it can be used as a data source for a flash chart.
xml 的例子是:
<areas>
<area title="AFGHANISTAN" mc_name="AF"></area>
<area title="ALAND ISLANDS" mc_name="AX"></area>
<area title="ALBANIA" mc_name="AL"></area>
<area title="ALGERIA" mc_name="DZ"></area>
<area title="ANDORRA" mc_name="AD"></area>
<area title="ANGOLA" mc_name="AO"></area>
<area title="ANGUILLA" mc_name="AI"></area>
<area title="ANTIGUA AND BARBUDA" mc_name="AG"></area>
推荐答案
以下是处理 XML 的示例:
Here's an example of how you can process the XML:
var xml = @"<areas>
<area title=""AFGHANISTAN"" mc_name=""AF""></area>
<area title=""ALAND ISLANDS"" mc_name=""AX""></area>
<area title=""ALBANIA"" mc_name=""AL""></area>
<area title=""ALGERIA"" mc_name=""DZ""></area>
<area title=""ANDORRA"" mc_name=""AD""></area>
<area title=""ANGOLA"" mc_name=""AO""></area>
<area title=""ANGUILLA"" mc_name=""AI""></area>
<area title=""ANTIGUA AND BARBUDA"" mc_name=""AG""></area>
</areas>
";
var doc = new XmlDocument();
doc.LoadXml(xml);
var nodes = doc.SelectNodes("areas/area");
foreach (XmlNode node in nodes)
{
// You can view existing attribute values through node.Attributes.
var att = doc.CreateAttribute("value");
att.Value = "something";
node.Attributes.Append(att);
}
Console.WriteLine(doc.OuterXml);
这篇关于读取 XML 模板,进行更改并显示在页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文