C# XML - XmlNode and XmlAttribute

时间:2024-04-18 03:58:48
         public static string TestXML(string path)
{ XmlDocument doc = new XmlDocument();
doc.Load(path); XmlNode root = doc.FirstChild;
ParseXmlNode(root); XmlNode nextNode = root.NextSibling;
while (nextNode != null)
{
ParseXmlNode(nextNode);
nextNode = nextNode.NextSibling;
} return "";
} public static void ParseXmlNode(XmlNode root)
{
if (root == null)
{
return;
} bool hasChild = root.HasChildNodes;
if (hasChild)
{
for (int i = ; i < root.ChildNodes.Count; i++)
{
XmlNode childnode = root.ChildNodes[i]; XmlAttributeCollection attrlist = childnode.Attributes;
XmlAttribute kifattr = null;
string childNodeName = childnode.Name;
if (attrlist != null)
{
kifattr = attrlist["KifSchema"];
}
if (kifattr != null)
{
Console.WriteLine(childNodeName + "\t" + kifattr);
} ParseXmlNode(childnode);
}
}
else
{
Console.WriteLine("name is: " + root.Name + " value is: " + root.InnerText);
//Console.WriteLine();
}
}