关于XML的读写问题!!

whhitli 2008-12-15 08:16:39
<?xml version="1.0" encoding="utf-8" ?>
<book>
<lovel>
<name>jim</name>
<author>罗曼罗兰</author>
</lovel>
<lovel>
<name>lucy</name>
<author>杰克逊</author>
</lovel>
</book>

我可以根据"jim"找到"罗曼罗兰"这个信息吗?
要是有的话,怎么的一个读法,还有就是怎么添加节点!!!

我刚刚接触XML,急着用,没办法,请各位帮忙!!!!!
...全文
138 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
daxiaxl 2008-12-26
  • 打赏
  • 举报
回复
ding
hangang7403 2008-12-15
  • 打赏
  • 举报
回复
static string Search()
{
string author="";
XmlDocument doc = new XmlDocument();
doc.Load("你XML文件的路径");
XmlNodeList nodelist = doc.SelectSingleNode("book").ChildNodes;
foreach (XmlNode xn in nodelist)
{
string name = xn.SelectSingleNode("name").InnerText;
if(name.equal("jim"))
{
author= xn.SelectSingleNode("author").InnerText;
}
}
return author;
}
lvg 2008-12-15
  • 打赏
  • 举报
回复
up
whhitli 2008-12-15
  • 打赏
  • 举报
回复
static string Search()
{

string author = "";
XmlDocument doc = new XmlDocument();
doc.Load("I:\\book.xml");
XmlNodeList nodelist = doc.SelectSingleNode("book").ChildNodes;
XmlNode node = doc.SelectSingleNode("/book/lovel[name='jim']/author");
foreach (XmlNode xn in nodelist)
{
string name = node.SelectSingleNode("name").InnerText;
if (Object.Equals(name, "jim"))
{
author = node.SelectSingleNode("author").InnerText;
}
}
return author;
}

string name = node.SelectSingleNode("name").InnerText;
这句话老出错!!
说什么"new"关键字创建对象实例;调用前检查确定对象是否为空!!

我把它先定义String name = "name";
还是不行!!

怎么回事???
whhitli 2008-12-15
  • 打赏
  • 举报
回复


12楼正解,不过还是要改一个地方:
name.equal("jim") 改为 name.Equals("jim")



谢谢各位的热心帮忙

结贴,散分喽!!!!
哈哈哈
claymore1114 2008-12-15
  • 打赏
  • 举报
回复
ding
whhitli 2008-12-15
  • 打赏
  • 举报
回复

二楼的大虾请回答:
说node没有定义,应该是什么类型的?

还有就是你的一点错误,String中不含equal()而是Equals()
ws_hgo 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 net_lover 的回复:]


XmlNode node = doc.SelectSingleNode("/book/lovel[name='jim']/author");
node.InnerText就是
[/Quote]
用老大的
bingbingw 2008-12-15
  • 打赏
  • 举报
回复
呵呵~
wb186 2008-12-15
  • 打赏
  • 举报
回复
都学习啦 呵呵
孟子E章 2008-12-15
  • 打赏
  • 举报
回复


XmlNode node = doc.SelectSingleNode("/book/lovel[name='jim']/author");
node.InnerText就是
HDNGO 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liu2008hz 的回复:]
用我这个,包你搞定,接分~~

C# code
static string Search()
{
string author="";
XmlDocument doc = new XmlDocument();
doc.Load(@"你XML文件的路径");
XmlNodeList nodelist = doc.SelectSingleNode("book").ChildNodes;
foreach (XmlNode xn in nodelist)
{
string name = node.SelectSingl…
[/Quote]

hehe ~
Hertz_liu 2008-12-15
  • 打赏
  • 举报
回复
用我这个,包你搞定,接分~~

static string Search()
{
string author="";
XmlDocument doc = new XmlDocument();
doc.Load(@"你XML文件的路径");
XmlNodeList nodelist = doc.SelectSingleNode("book").ChildNodes;
foreach (XmlNode xn in nodelist)
{
string name = node.SelectSingleNode("name").InnerText;
if(name.equal("jim"))
{
author= node.SelectSingleNode("author").InnerText;
}
}
return author;
}
jhdxhj 2008-12-15
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<MotherTongue lalTitle = "24小时天气预报"
lalWeatherType = "天气类型"
lalMax = "最高温度"
lalMin = "最低温度"
btnSet="设置"
title="天气预报"
btnCancel="取消"
>
</MotherTongue>
<English lalTitle = "24-hour weather forecast"
lalWeatherType = "weather type"
lalMax = "maximum temperature"
lalMin = "minimum temperature"
btnSet="set"
title="weather forecast"
btnCancel="cancel"
>
</English>
</configuration>
void LoadCfgFile()
{

XmlDocument x = new XmlDocument();
x.Load("LanguageXml/WeatherForm.xml");
switch (MainForm._nLanguage)
{
case 1:
XmlNodeList xnl = x.SelectNodes("/configuration/MotherTongue");
this.lalMax.Text = xnl[0].Attributes["lalMax"].InnerText;
this.lalMin.Text = xnl[0].Attributes["lalMin"].InnerText;
this.lalTitle.Text = xnl[0].Attributes["lalTitle"].InnerText;
this.btnSet.Text = xnl[0].Attributes["btnSet"].InnerText;
this.btnCancel.Text = xnl[0].Attributes["btnCancel"].InnerText;
this.lalWeatherType.Text = xnl[0].Attributes["lalWeatherType"].InnerText;
this.Text = xnl[0].Attributes["title"].InnerText;




break;
case 2:
XmlNodeList xnl1 = x.SelectNodes("/configuration/English");
this.lalMax.Text = xnl1[0].Attributes["lalMax"].InnerText;
this.lalMin.Text = xnl1[0].Attributes["lalMin"].InnerText;
this.lalTitle.Text = xnl1[0].Attributes["lalTitle"].InnerText;
this.btnSet.Text = xnl1[0].Attributes["btnSet"].InnerText;
this.btnCancel.Text = xnl1[0].Attributes["btnCancel"].InnerText;
this.lalWeatherType.Text = xnl1[0].Attributes["lalWeatherType"].InnerText;
this.Text = xnl1[0].Attributes["title"].InnerText;
break;

}
}

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧