a.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<root>
<a a1="1" a2="1">
<b b1="1">b</b>
abc
<c c1="1">c</c>
abcd
<d d1="1">
<e e1="1">e</e>
</d>
abcde
</a>
<a a1="2" a2="2">
<b b1="1">b</b>
abc
<c c1="1">c</c>
abc
<d d1="1">
<e e1="1">e</e>
</d>
abcde
</a>
</root>
用.net 取出文档中 a 节点的属性 和 b c d 节点以及文本节点 打印结果是 nodetype nodevalue
帮帮忙看看好吗?
<?xml version="1.0" encoding="utf-8" ?>
<root>
<a a1="1" a2="1">
<b b1="1">b</b>
abc
<c c1="1">c</c>
abcd
<d d1="1">
<e e1="1">e</e>
</d>
abcde
</a>
<a a1="2" a2="2">
<b b1="1">b</b>
abc
<c c1="1">c</c>
abc
<d d1="1">
<e e1="1">e</e>
</d>
abcde
</a>
</root>
用.net 取出文档中 a 节点的属性 和 b c d 节点以及文本节点 打印结果是 nodetype nodevalue
帮帮忙看看好吗?
doc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication2\XMLFile3.xml");
XmlNode node = doc.SelectSingleNode("/root/a");
foreach (XmlNode el in node.ChildNodes)
{
if (el.Name != "e")
Debug.WriteLine(el.Name + " " + el.NodeType + " " + el.InnerText);
}
参见.net sdk文档。或者贴出你的代码
Console.WriteLine( string.Format( "nodetype:{0},name:{1}", xpn.NodeType, xpn.Name ) );//XPathNavigator 相当于指针
}
我这里有两种方法:
1
xmldocument xmldoc=new xmldocument();
xmldoc.load("xml文件路径");
foreach( XPathNavigator xpn in xmldoc.CreateNavigator().SelectSingleNode( "/roots/root/a[1]" ).Select( "(./namespace::*)|(./attribute::*)|(./node())" ) ) {//node 即代表元素也代表文本
Console.WriteLine( string.Format( "nodetype:{0},name:{1},value{2}", xpn.NodeType, xpn.Name ,xpn.Value.Trim()) );//XPathNavigator 相当于指针
}2。
XmlDocument x = new XmlDocument();
x.Load( "C:\\Documents and Settings\\Dev6\\Desktop\\BELINDA\\Solution1\\xml.xml" );//加载文档
XmlNode n = x.SelectSingleNode( "//roots" );//确定指定节点(为上一根节点) 如:(<roots><>)
foreach( XmlNode xm in n.ChildNodes )//
{
Console.WriteLine( xm.Name + "==>" + xm.NodeType );
if( xm.Attributes != null ) {
foreach( XmlAttribute att in xm.Attributes ) {
Console.WriteLine( att.Name + "==>" + att.NodeType + "==>" + att.Value );
}
}
XmlNodeList nl = xm.ChildNodes;
foreach( XmlNode no in nl ) {
XmlNodeList xml = no.ChildNodes;
if( no.ChildNodes.Count != 0 ) {
Console.WriteLine( no.Name + "==>" + no.NodeType );
} else {
Console.WriteLine( no.Name + "==>" + no.NodeType + "==>" + no.InnerText.Trim() );
}
}
}