xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<result name="response" numFound="4" start="0">
<doc>
<str name="id">testdoc</str>
</doc>
<doc>
<str name="author">hello</str>
<str name="id">123</str>
<arr name="title">
<str>123</str>
</arr>
</doc>
<doc>
<str name="author">world</str>
<str name="id">234</str>
<arr name="title">
<str>234</str>
</arr>
</doc>
<doc>
<str name="author">please</str>
<str name="id">345</str>
<arr name="title">
<str>345</str>
</arr>
</doc>
</result>请问如何将读取每个doc下面的author、id、title,请用C#,万分感谢!
<?xml version="1.0" encoding="UTF-8"?>
<result name="response" numFound="4" start="0">
<doc>
<str name="id">testdoc</str>
</doc>
<doc>
<str name="author">hello</str>
<str name="id">123</str>
<arr name="title">
<str>123</str>
</arr>
</doc>
<doc>
<str name="author">world</str>
<str name="id">234</str>
<arr name="title">
<str>234</str>
</arr>
</doc>
<doc>
<str name="author">please</str>
<str name="id">345</str>
<arr name="title">
<str>345</str>
</arr>
</doc>
</result>请问如何将读取每个doc下面的author、id、title,请用C#,万分感谢!
解决方案 »
- 为什么WIN7下DirectShow摄像头打不开
- datagridview如何用代码添加新的空行?
- c#如何控制MouseMove事件的移动区域 急....
- 求 程序天下ASP.NET 3.5网络数据库开发实例自学手册 光盘中的一段代码
- c#调用sybase存储过程,接返回数据集问题,有代码,请大家帮忙,谢谢
- 一个“采用.Net Socket技术的在线聊天室”的问题.希望使用过的朋友进来聊聊
- 100分跪求 谢谢!
- wpf中如何保存已经与控件绑定的结构体中的数据到文件中。
- 关于全局变量问题。
- 关于textbox的一些问题
- 怎么快速删除DataGridView里面的重复列?
- 以数字个数分隔字符,求助
http://msdn.microsoft.com/en-us/library/bb387098.aspx
XDocument doc = XDocument.Load("c:\\1.txt");
var vs = doc.Descendants("doc");
foreach (var v in vs)
{
foreach (XElement item in v.Nodes())
{
MessageBox.Show(item.Value);
}
}
我需要遍历所有doc,但对于doc下面的author等值要准确定位,谢谢
void Main()
{
XDocument xml=XDocument.Load("c:\\test.xml");
var query=from x in xml.Descendants("doc")
select new
{
author=x.Elements("str").Any(s=>s.Attribute("name").Value=="author")?x.Elements("str").FirstOrDefault(z=>z.Attribute("name").Value=="author").Value:"",
id=x.Elements("str").Any(s=>s.Attribute("name").Value=="id")?x.Elements("str").FirstOrDefault(z=>z.Attribute("name").Value=="id").Value:"",
title=x.Elements("str").Any(s=>s.Attribute("name").Value=="title")?x.Elements("str").FirstOrDefault(z=>z.Attribute("name").Value=="title").Value:"",
};
foreach(var item in query)
Console.WriteLine("author:{0}\tid:{1}\ttitle:{2}",item.author,item.id,item.title);
}
author:hello id:123 title:
author:world id:234 title:
author:please id:345 title:
foreach (XElement r in XElement.Load(Server.MapPath("~/XMLFile1.xml")).Elements("doc"))
{
var idobj=r.Elements("str").Where(c => c.Attribute("name").Value=="id");
var titleobj=r.Elements("arr").Where(c => c.Attribute("name").Value == "title");
var authorobj = r.Elements("str").Where(c => c.Attribute("name").Value == "author");
string id="", author="", title = "";
if (idobj.ToList().Count!= 0) id = idobj.First().Value;
if (titleobj.ToList().Count != 0) title = titleobj.First().Value;
if (authorobj.ToList().Count != 0) author = authorobj.First().Value; Response.Write("id:" + id + "titile:" + title + "author:" + author + "<br>"); }
id:testdoctitile:author:
id:123titile:123author:hello
id:234titile:234author:world
id:345titile:345author:please
考虑下易读及操作性将xml结构稍稍改下,
就很容易操作了,如:
1.<?xml version="1.0" encoding="UTF-8"?>
<result name="response" numFound="4" start="0">
<doc id="testdoc">
<author>hello</author>
<id>123</id>
<title>123</title>
</doc>
<doc id="testdoc2">
<author>world</author>
<id>234</id>
<title>234</title>
</doc>
<doc id="testdoc3">
<author>please</author>
<id>345</id>
<title>345</title>
</doc>
</result>2.
<?xml version="1.0" encoding="UTF-8"?>
<result name="response" numFound="4" start="0">
<doc id="testdoc">
<book author="hello" id="123" title="123"></book>
</doc>
<doc id="testdoc2">
<book author="world" id="234" title="234"></book>
</doc>
<doc id="testdoc3">
<book author="please" id="345" title="345"></book>
</doc>
</result>
doc.Load(System.AppDomain.CurrentDomain.BaseDirectory + "a.xml");
XmlNodeList xmllist = doc.SelectSingleNode("result").ChildNodes;
for (int i = 0; i < xmllist.Count; i++)
{
XmlNodeList c_xmllist = xmllist[i].ChildNodes;
for (int j = 0; j < c_xmllist.Count; j++)
{
Console.WriteLine(c_xmllist[j].Attributes[0].Value + ":" + c_xmllist[j].InnerText);
}
}
Console.ReadLine();
请问这个title怎么显示出来呢?
不是这个意思,我的意思是这个title的变量是空的,所以显示不出来