日期:2014-05-18  浏览次数:20847 次

如何读递归XML文档。
比如我有这样一个XML
<Object>
<Name>BB</Name>
<Object>
<Name>AA</Name>
<Object/>
</Object>

<Object>
<Name>CC</Name>
<Object/>
</Object>
</Object>


如何按顺序读出3个Object。顺序如下:AA-〉BB-〉CC

------解决方案--------------------
try
{
if (nxml.Name == "Name")
{
al.Add(nxml.InnerText);
}

foreach (XmlNode child in nxml.ChildNodes)
{
BindXmlToTreeView(child,ref al);
}
}
catch (Exception ex)
{
throw (ex);
}
随便写的,没测试.
------解决方案--------------------
XmlDocument xdoc = new XmlDocument();
xdoc.Load("FileName");
ArrayList al = new ArrayList();
this.BindXmlToTreeView(xdoc.DocumentElement, ref al);
 
  
private void BindXmlToTreeView(XmlNode nxml,ref ArrayList al)
{
try
{
if (nxml.Name == "Name")
{
al.Add(nxml.InnerText);
}

foreach (XmlNode child in nxml.ChildNodes)
{
BindXmlToTreeView(child,ref al);
}
}
catch (Exception ex)
{
throw (ex);
}
}

刚没发完.
------解决方案--------------------
你可以先读出来然后排序
例如如下
C# code

XmlDocument doc = new XmlDocument();
doc.Load("e:\\1.xml");
XmlNodeList list = doc.SelectNodes("//Name");
List<string> sorted = new List<string>();
foreach (XmlNode node in list)
{
    sorted.Add(node.InnerText);
}
sorted.Sort();
foreach (string s in sorted)
{
    Console.WriteLine(s);//这里就是按照你要的顺序
}

------解决方案--------------------
[Quote=引用:]
你可以先读出来然后排序 
例如如下 

C# code
XmlDocument doc = new XmlDocument();
doc.Load("e:\\1.xml");
XmlNodeList list = doc.SelectNodes("//Name");
List<string> sorted = new List<string>();
foreach (XmlNode node in list)
{
sorted.Add(node.InnerText);
}
sorted.Sort();
foreach (string s in sorted)
{
Console.WriteLine(s);//这里就是按照你要的顺序
}


mark ;