نحوه کار با فایل XML در #C
فایل XML
<?xml version="1.0" encoding="utf-8" ?>
<items>
<item>
<Key>123</Key>
</item>
<item>
<Key>451</Key>
</item>
</items>
حال در سی شارپ اینطوری صداش می زنیم و از آن استفاده می کنیم.
XmlNode xmlNode;
XmlDocument xmlDoc = new XmlDocument();
String htmlCode = string.Empty;
WebClient htmlDoc = new WebClient();
string AddresXml = HttpContext.Current.Server.MapPath(@"\App_Data\APIKeys.xml");
htmlCode = htmlDoc.DownloadString(AddresXml);
//htmlCode = htmlCode.Substring(htmlCode.IndexOf("<table class=\"comodities\" style=\"margin-top:4px;\">"), htmlCode.IndexOf("</table></center><br>") - htmlCode.IndexOf("<table class=\"comodities\" style=\"margin-top:4px;\">") + 8);
htmlCode = htmlCode.Replace("'", "\"");
htmlCode = htmlCode.Replace("</a>", "");
xmlDoc.LoadXml(htmlCode);
for (int i = 0; i < xmlDoc.ChildNodes.Count; i++)
{
if (xmlDoc.ChildNodes[i].Name == "items")
{
for (int j = 0; j < xmlDoc.ChildNodes[i].ChildNodes.Count; j++)
{
if (xmlDoc.ChildNodes[i].ChildNodes[j].Name == "item")
{
key = Convert.ToString(xmlDoc.ChildNodes[i].ChildNodes[j].ChildNodes[0].InnerText);
Guid.TryParse(key, out apiKey);
OBJGuid.Add(apiKey);
}
}
}
}