比如以下xml
<books>
<book>
<title>XML</title>
<publisher>wrox</Publisher>
<book>
<publisher>tsinghua</publisher>
</book>
<book>
<Title>C# framework</title>
</book>
<books>用下面的程序读
XmlNodeList nodes7 = doc.SelectNodes(@"books/book");
for (int i = 0; i < nodes7.Count; i++)
{
if (nodes7[i].HasChildNodes)
{
for (int m = 0; m < nodes7[i].ChildNodes.Count; m++)
{
if (nodes7[i].ChildNodes[m].Name == "title")
{
strtitle= nodes7[i].ChildNodes[m].InnerText;
}
if (nodes7[i].ChildNodes[m].Name == "publisher")
{
strpublisher= nodes7[i].ChildNodes[m].InnerText;
}
}
}第二个book没有title节点,但他以第一个wrox在读,第三个book没有publisher,但输出的却是第二个tsinghua.我想以book循环读取book下的每一个节点并赋值给变量,应该如何去做????
<books>
<book>
<title>XML</title>
<publisher>wrox</Publisher>
<book>
<publisher>tsinghua</publisher>
</book>
<book>
<Title>C# framework</title>
</book>
<books>用下面的程序读
XmlNodeList nodes7 = doc.SelectNodes(@"books/book");
for (int i = 0; i < nodes7.Count; i++)
{
if (nodes7[i].HasChildNodes)
{
for (int m = 0; m < nodes7[i].ChildNodes.Count; m++)
{
if (nodes7[i].ChildNodes[m].Name == "title")
{
strtitle= nodes7[i].ChildNodes[m].InnerText;
}
if (nodes7[i].ChildNodes[m].Name == "publisher")
{
strpublisher= nodes7[i].ChildNodes[m].InnerText;
}
}
}第二个book没有title节点,但他以第一个wrox在读,第三个book没有publisher,但输出的却是第二个tsinghua.我想以book循环读取book下的每一个节点并赋值给变量,应该如何去做????
解决方案 »
- C#怎么给自己在图片上画的图形上添如长宽高的注释.
- 获取页面内容,很管用的一个技术!
- 帮忙看看一个LinQ的简单问题。
- 大哥哥,大姐姐,Winform程序里输入的10进制数字转化成16进制后如何显示到LED上面去?
- Vss 2005 的 Internet访问问题
- 画圆如何弄
- 如何实现数据库异步读取?
- .net与Oracle的连接字符串怎么写?就是那个协议不对,不知道为什么,请高手指点一下?
- 如何在Visual studio.net 2003中用C#语言做一个windows应用程序登录窗口?
- datagrid 里加了一列imageButton,但是无法响应事件
- Picture句柄问题
- 注册表部分信息不能保存?
http://www.cnblogs.com/thinhunan/archive/2006/10/11/525947.html
XmlElement xe = XmlElement(node) ;
OR
XmlElement xe = node as XmlElement ;
Then an XmlElement-based object is defined. sometimes,XmlElement is easy to use. If you want to get a attribute value of this node, you can use like this: xe.GetAttribute("publisher").
But for XmlNode, you should use like : node.Attributes["publisher"]
if()
需要
else
<books>
<book>
<title>XML</title>
<publisher>wrox</publisher>
</book>
<book>
<publisher>tsinghua</publisher>
</book>
<book>
<title>C# framework</title>
</book>
</books>";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);
System.Xml.XmlNodeList nodes = doc.SelectNodes("/books/book");
if (nodes != null)
{ string[] strTitle = new string[nodes.Count];
string[] strPublisher = new string[nodes.Count];
for (int i = 0 ; i < nodes.Count ; i++)
{
System.Xml.XmlNode titleNode = nodes[i].SelectSingleNode("title");
if (titleNode == null)
{
strTitle[i] = "";
}
else
{
strTitle[i] = titleNode.InnerText;
} System.Xml.XmlNode publisherNode = nodes[i].SelectSingleNode("publisher");
if (publisherNode == null)
{
strPublisher[i] = "";
}
else
{
strPublisher[i] = publisherNode.InnerText;
}
}
for (int i = 0 ; i < strTitle.Length ; i++)
{
Response.Write("<li>" + strTitle[i]);
}
Response.Write("<hr>");
for (int i = 0 ; i < strPublisher.Length ; i++)
{
Response.Write("<li>" + strPublisher[i]);
}
}
用xmltextreader吧, 在控制上比较好
<book>
<title id="01" author="AA">XML</title>
<title>VB.net</title>
<publisher>wrox</Publisher>
<book>
<publisher>tsinghua</publisher>
</book>
<book>
<Title>C# framework</title>
<Title>Dotnet framework</title>
</book>
<books>
"@author"就是属性