递归遍历XML节点属性和属性值

时间:2023-11-21 17:41:14
 public static XmlDocument FileMergedIntoXML(string strXmlPathPublic)
{
string strXmlPathPublic = string.Format(@"{0}file1.xml", AppDomain.CurrentDomain.BaseDirectory);
XmlDocument docPublic = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader readera = XmlReader.Create(strXmlPathPublic, settings);
docPublic.Load(readera) getXmlDocNode1(docPublic.DocumentElement); } /// <summary>
/// 递归遍历XML文件的节点和属性与属性值
/// </summary>
/// <param name="docPublicNode"></param>
private static void getXmlDocNode1(XmlNode docPublicNode)
{
string strNodeName = string.Empty;
string strAttributeName = string.Empty;
if (docPublicNode.HasChildNodes)
{
strNodeName = docPublicNode.Name;
XmlNodeList xmlnodelist = docPublicNode.ChildNodes; foreach (XmlNode xmlnode in xmlnodelist)
{
//Console.WriteLine("{0}", docPublicNode.Name.ToString());
if (xmlnode.Attributes.Count > )
{
foreach (XmlAttribute item in xmlnode.Attributes)
{
strNodeName = xmlnode.Name.ToString() + "=>"+ item.Name + "=>";
//Console.WriteLine(xmlnode);
Console.WriteLine("{0} ==>{1}==> {2}", strNodeName, item.Name, item.Value);
}
strAttributeName = string.Empty;
}
getXmlDocNode1(xmlnode);
}
strNodeName = string.Empty;
Console.WriteLine();
}
else
{ Console.WriteLine("{0}", docPublicNode.Name.ToString());
Console.WriteLine();
} }