Reading XML template, make a change and display on page
Hi I have a preformatted xml template file that lis开发者_Python百科ts all country codes.
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.
I then need to render the edited xml on the page so it can be used as a data source for a flash chart.
example of xml is:
<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>
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);
精彩评论