我有以下.xml,格式如此;
<imgdir name="Map.img">
<imgdir name="3rd">
<imgdir name="300000000">
<string name="streetName" value="Ellin Forest" />
<string name="mapName" value="Altaire Camp" />
</imgdir>
<imgdir name="300000100">
<string name="streetName" value="Altaire Camp" />
<string name="mapName" value="Small Forest" />
</imgdir>
</imgdir>
<imgdir name="MasteriaGL">
<imgdir name="610030010">
<string name="streetName" value="Crimsonwood Keep" />
<string name="mapName" value="Hall of Mastery" />
</imgdir>
我需要一个c#控制台来浏览每个imgdir名称,“3rd”,“MasteriaGL”等(还有更多的名字),并提供输出;
ID: 300000000 NAME: 3rd : Ellin Forest : Altaire Camp
ID: 300000100 NAME: 3rd : Altaire Camp : Small Forest
ID: 610030010 NAME: MasteriaGL : Crimsonwood Keep : Hall of Mastery
答案 0 :(得分:0)
您可以阅读XML
例如:
<?xml version="1.0" encoding="UTF-8"?>
<Home>
<TextEntries>
<follow>
<item visible="true" navid="fb" url="https://www.facebook.com/" type="text" hideineditor="false" esbname="facebook Follow Link" linktype="new" id="200038"><![CDATA[facebook]]></item>
<item visible="true" navid="twitter" url="https://twitter.com/" type="text" hideineditor="false" esbname="twitter Follow Link" linktype="new" id="200039"><![CDATA[twitter]]></item>
</follow>
</TextEntries>
</Home>
阅读节点
XmlElement root = null;
XmlDocument doc = new XmlDocument();
doc.Load(sourceXmlFilePath);
root = doc.DocumentElement;
XmlNode rootnode = root.SelectSingleNode("//Home//TextEntries");
XmlNode followNode = root.SelectSingleNode("//Home//TextEntries//follow");
string myAttribute=followNode.ChildNodes[0].Attributes["id"].Value;//this will read the attribute ID of first inner element
string myFirstInnerNodeValue=followNode.ChildNodes[0].InnerText;//this will read the data of first inner node i.e facebook
希望这能帮到你!
答案 1 :(得分:0)
你可以试试这个:
private static string GetString(XmlNode root)
{
string retStr = "";
XmlNodeList nodes = root.SelectNodes("imgdir");
if (nodes.Count != 0)
{
foreach (XmlNode node in nodes)
{
retStr=retStr+"\r\n"+GetString(node);
}
}
else
{
string street = root.SelectSingleNode("string[@name='streetName']").Attributes["value"].Value;
string map = root.SelectSingleNode("string[@name='mapName']").Attributes["value"].Value;
string id = root.Attributes["name"].Value;
retStr ="ID " + id+":"+"NAME "+root.ParentNode.Attributes["name"].Value+":"+ street + ":" + map;
}
return retStr;
}
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("Stack.xml");
XmlNodeList nodes = doc.SelectNodes("imgdir/imgdir");
foreach(XmlNode node in nodes)
{
Console.WriteLine(GetString(node));
}
Console.ReadLine();
}
希望,它会对你有帮助。
答案 2 :(得分:0)
假设<imgdir name="Map.img">
是实际XML的根元素,您可以这样做:
var file = XElement.Load("path_to_xml_file.xml");
var secondLevelImgdirs = file.Elements("imgdir");
foreach(var imgDir2 in secondLevelImgdirs)
{
var name = (string)imgDir2.Attribute("name");
var thirdLevelImgdirs = imgDir2.Elements("imgdir");
foreach(var imgDir3 in thirdLevelImgdirs)
{
var id = (string)imgDir3.Attribute("name");
var streetName = (string)imgDir3.Elements("string")
.FirstOrDefault(o => "streetName" == (string)o.Attribute("name"));
var mapName = (string)imgDir3.Elements("string")
.FirstOrDefault(o => "mapName" == (string)o.Attribute("name"));
Console.WriteLine("ID: {0} NAME: {1} : {2} : {3}", id, name, streetName, mapName);
}
}