用.net的XMLNode可以读取节点,但是对于下面这种attribute该怎么读呢? <user id="03">
<from>"MC"</from>
<to>"EA"</to>
<attribute name="aa" length="10" datamap="aamap" />
<attribute name="bb" length="11" datamap="bbmap" istrue = "false"/>
</user>
其中attribute的数量可以是任意条
<from>"MC"</from>
<to>"EA"</to>
<attribute name="aa" length="10" datamap="aamap" />
<attribute name="bb" length="11" datamap="bbmap" istrue = "false"/>
</user>
其中attribute的数量可以是任意条
for(int i = 0;i<NodeList.Count;i++)
{
XmlNode Node = NodeList[i];
if (i == index)
{
//加载根结点的树
//判断是否为子树的依据是根据xsd架构文件的属性确定的,
//含有子树的结点(元素)是包含属性的,这个不是通用的xml解析程序
TreeNode bootNode = null;
if (Node.Attributes.Count >0)
{
foreach(XmlAttribute att in Node.Attributes)
{
bootNode= new TreeNode(att.InnerXml,5,5);
tv.Nodes.Add(bootNode);
}
}
if (bootNode!=null)
LoadTree(bootNode,Node);
break;
}
}
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %><html>
<script language="C#" runat="server"> protected void Page_Load(Object Src, EventArgs E) {
DataSet ds = new DataSet(); FileStream fs = new FileStream(Server.MapPath("myXml.xml"),FileMode.Open,FileAccess.Read); //myXml.xml是要读取的xml文档
StreamReader reader = new StreamReader(fs);
ds.ReadXml(reader);
fs.Close(); DataView Source = new DataView(ds.Tables[0]); MyLiteral.Text = Source.Table.TableName;
MyDataGrid.DataSource = Source;
MyDataGrid.DataBind();
}</script><body> <h3><font face="宋体">表的 XML 数据:<asp:Literal id="MyLiteral" runat="server" /></font></h3> <ASP:DataGrid id="MyDataGrid" runat="server"
Width="900"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="宋体"
Font-Size="9pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/></body>
</html>
xmldoc.LoadXml("<user ...</user>");第一种:
foreach(XmlNode node in xmldoc.DocumentElement.ChildNodes)
{
if(node.Name == "attribute")
{
//这里的 node 就是你想要的东东
}
}第二种:foreach(XmlNode node in doc.selectnodes("/user/attribute"))
{
//这里的 node 就是你想要的东东
}
or
node.Attributes["Name"]