『50分』C# XML文件操作方法

vitachuh 2009-04-02 06:12:52
Xml文件内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<com>
<user id="admin">
<option>aaa</option>
<option>bbb</option>
<option>ccc</option>
</user>
<user id="111">
<option>ddd</option>
<option>eee</option>
<option>fff</option>
</user>
</com>
<date>1111-11-11</date>
</root>


我是用的XmlDocument的SelectNodes进行遍历的,用起来感觉有点怪。。。是不是效率太低了?而且不知道怎么通过Attribute来查询节点。。。各种尝试都失败了。。。

现特请教如下问题:
XmlDocument中的GetElementById中,id对应上述xml文件中的什么?TagName、EntityNode呢?
怎样快速读出date项的文本值?
怎样快速读出id为111的user的所有option?又怎样添加一个有若干option的id为234的user呢?

请给出关键的代码,感激不尽!

...全文
972 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattiger75 2009-04-28
  • 打赏
  • 举报
回复
若要使用getElementById方法,必须在XML文件中先指明哪个attribute是ID,否则得到的为null.
如:
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE cqustlib
[
.
.
.
<!ELEMENT user ANY>
<!ATTLIST user id ID #REQUIRED>
.
.
.
]
>
vitachuh 2009-04-17
  • 打赏
  • 举报
回复
顶一个先~~
jdhlowforever 2009-04-03
  • 打赏
  • 举报
回复
mark@!
vitachuh 2009-04-03
  • 打赏
  • 举报
回复
谢谢大家的热心回复和帮助。

还有些不明白的地方:

[Quote=引用 5 楼 Sandy945 的回复:]
admin 111 就是GetElementById 需要的id

怎样快速读出id为111的user的所有option?
XmlDocument.GetElementById("111").GetElementsByTagName("date")
XmlDocument.GetElementById("").ChildNodes
[/Quote]

参照Sandy945的写法如下:


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("test.xml");

XmlElement e = xmlDoc.GetElementById("111");
XmlNodeList nodes = e.GetElementsByTagName("option");


结果e是null呢?没找到。。。是不是我哪儿写错了?还是GetElementById不是这样用的?
以上代码请不要考虑低级出错的原因,请从实际问题考虑,谢谢。
gui0605 2009-04-02
  • 打赏
  • 举报
回复

private static string appPath = Environment.CurrentDirectory + "\\PhoneSystemClient.exe.config";
private XmlDocument configData = new XmlDocument();
configData.Load(appPath);
configData.SelectSingleNode("/configuration/appSettings/add[@key=\"phoneNumber\"]").Attributes["value"].Value = textBox1.Text;
configData.Save(appPath);
wuyq11 2009-04-02
  • 打赏
  • 举报
回复
查询XML用LINQ to xml
XDocument adList = XDocument.Load(Server.MapPath("Data.xml"));
var ad = from a in adList.Descendants("root").Elements("com")
select new
{
image = a.Attribute("A").Value,
link = a.Attribute("B").Value,
title = a.Attribute("C").Value
};
string s = "";
foreach (var a in ad)
s += a.image
http://www.cnblogs.com/yintian2/archive/2007/09/13/891932.html
Garnett_KG 2009-04-02
  • 打赏
  • 举报
回复
Linq to XML



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument xdoc = XDocument.Load(@"C:\csdn.xml");

var des = from x in xdoc.Descendants()
where x.Name=="user" && x.Attribute("id").Value=="admin"
select x;

foreach (var t in des)
{
Console.WriteLine(t);
}
Console.Read();
}
}
}




阿非 2009-04-02
  • 打赏
  • 举报
回复
<user id="admin">
<user id="111">

admin 111 就是GetElementById 需要的id


<root>
<com>
<user >

root com user 就是TagName

怎样快速读出date项的文本值?

XmlDocument.SelectSingleNode(/root/date)
XmlDocument.SelectSingleNode(root/date)
XmlDocument.SelectSingleNode(/root//date)

XmlDocument.GetElementsByTagName("date")[0]

怎样快速读出id为111的user的所有option?
XmlDocument.GetElementById("111").GetElementsByTagName("date")
XmlDocument.GetElementById("").ChildNodes

又怎样添加一个有若干option的id为234的user呢?

XmlElement xUser = new XmlElement();
xUser.SetAttribute("id", "234");
xUser.Name="user";
for (int i = 0; i < 10; i++)
{
XmlElement xe = new XmlElement();
xe.Name = "option";
xe.InnerText = "ddd";

xUser.AppendChild(xe);
}
XmlDocument.AppendChild(xUser);
yinxiaowei823 2009-04-02
  • 打赏
  • 举报
回复
路过
birdlonger 2009-04-02
  • 打赏
  • 举报
回复
string xmlpath = @"d:\CSDN_2009.04.02_XML.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlpath);
XmlElement root = doc.DocumentElement;
XmlNode comnode = root.SelectSingleNode("com");
foreach (XmlNode n in comnode.ChildNodes)
{
if (n.Attributes.GetNamedItem("id").Value == "111") // id
{
Console.WriteLine(n.InnerXml.ToString());
}
}
XmlElement node = doc.CreateElement("user");
node.SetAttribute("id", "234");
XmlElement childnode1 = doc.CreateElement("option");
childnode1.InnerText = "雁子";
XmlElement childnode2 = doc.CreateElement("option");
childnode2.InnerText = "雁子";
XmlElement childnode3 = doc.CreateElement("option");
childnode3.InnerText = "雁子";
node.AppendChild(childnode1);
node.AppendChild(childnode2);
node.AppendChild(childnode3);
comnode.AppendChild(node);
doc.Save(xmlpath);
lovelan1748 2009-04-02
  • 打赏
  • 举报
回复
XmlTextReader xr = new XmlTextReader (Path);
while(xr.Read())
{
if(xr.NodeType==XmlNodeType.Element && xr.Name == "Date")
{
Console.WriteLine(xr.Value);
}
}
liu4545533 2009-04-02
  • 打赏
  • 举报
回复
在这里面有啊 希望能够帮助你http://tieba.baidu.com/f?kz=78710893

110,547

社区成员

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

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

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