C# 读取XML每个节点的值

Eric290 2009-04-02 04:37:29
XML文档结构如下:
<?xml version="1.0" encoding="utf-8"?>
<packet>
<proto name="1" showname="1">
<field name="1-1" showname="1-1" show="1-1"/>
<field name="1-2" showname="1-2" show="1-2"/>
<field name="1-3" showname="1-3" show="1-3"/>
<field name="1-4" showname="1-4" show="1-4"/>
</proto>
<proto name="2" showname="2">
<field name="2-1" showname="2-1" show="2-1"/>
<field name="2-2" showname="2-2" show="2-2"/>
<field name="2-3" showname="2-3" show="2-3"/>
<field name="2-4" showname="2-4" show="2-4"/>
<field name="2-5" showname="2-5" show="2-5"/>
<field name="2-6" showname="2-6" show="2-6"/>
<field name="2-7" showname="2-7" show="2-7"/>
<field name="2-8" showname="2-8" show="2-8"/>
</proto>
<proto name="3" showname="3">
<field name="3-1" showname="3-1" show="3-1">
<field name="3-1-1" showname="3-1-1" show="3-1-1"/>
<field name="3-1-2" showname="3-1-2" show="3-1-2"/>
<field name="3-1-3" showname="3-1-3" show="3-1-3"/>
</field>
<field name="3-2" showname="3-2" show="3-2">
<field name="3-2-1" showname="3-2-1" show="3-2-1"/>
<field name="3-2-2" showname="3-2-2" show="3-2-2"/>
<field name="3-2-3" showname="3-2-3" show="3-2-3"/>
</field>
</proto>
<proto name="4" showname="4">
<field name="4-1" value="4-1"/>
<field name="4-2" showname="4-2" show="4-2">
<file name="4-2-1" showname="4-2-1" show="4-2-1"/>
</field>
</proto>
</packet>
C#读取该XML每个节点的showname 和 show的值, 没有要读取value的值,我被循环弄混头脑,希望得到帮助...谢谢





...全文
6108 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cz0608 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
不好意思,少说了一点...每个节点都要都是一个变量...我要一层一层读下来,不能递归...
因为我要先读取全部的数据,再将自己需要的放入datatable中,再绑定datagrid
[/Quote]
楼主你好,目前我也遇到同样的问题,你能把怎么读取XML每个节点,以及放入dataTable中,这样一个过程的代码发给我一份吗?万分感谢!邮箱:530332388@qq.com
kele_zm 2012-04-13
  • 打赏
  • 举报
回复
可以给我一份吗?我邮箱zhangmeng7212@163.com急用
tly2005 2012-02-20
  • 打赏
  • 举报
回复
学习中,
wo123deshenghuo 2011-08-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 youliting290 的回复:]

Datatable是数据库的表结构,不是treeview那种结构
[/Quote]
可以把修改过的XMLHelp给我一份吗?我的邮箱是pengzejiangoo@163.com
谢谢
SoaringSnake 2009-05-06
  • 打赏
  • 举报
回复
[size=12px]为什么必须要用dataTable呢?本人酷爱泛型集合~嘿嘿[size]
Joson.e8love 2009-04-16
  • 打赏
  • 举报
回复
e8sp.com@139.com
Thanks! createTim

发份吧
Eric290 2009-04-03
  • 打赏
  • 举报
回复
谢谢大家的帮助,俺昨晚睡了一觉,头脑清醒多了...问题解决了....结贴!!!
Eric290 2009-04-02
  • 打赏
  • 举报
回复
zs.280@163.com
Thanks!
createTim 2009-04-02
  • 打赏
  • 举报
回复
去下一个XMLHelp
修改一下就可以了!我这里有改好了的如果你要,给我邮箱!
Eric290 2009-04-02
  • 打赏
  • 举报
回复
Datatable是数据库的表结构,不是treeview那种结构
我姓区不姓区 2009-04-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 youliting290 的回复:]
不好意思,少说了一点...每个节点都要都是一个变量...我要一层一层读下来,不能递归...
因为我要先读取全部的数据,再将自己需要的放入datatable中,再绑定datagrid

[/Quote]
最终是要生成DataTable?DataTable结构是咋样的
Eric290 2009-04-02
  • 打赏
  • 举报
回复
不好意思,少说了一点...每个节点都要都是一个变量...我要一层一层读下来,不能递归...
因为我要先读取全部的数据,再将自己需要的放入datatable中,再绑定datagrid
我姓区不姓区 2009-04-02
  • 打赏
  • 举报
回复
咋又发了一次:

static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("E:\\test.xml");
foreach (XmlNode node in doc.GetElementsByTagName("packet"))
GetNodes(node);
}
static void GetNodes(XmlNode node)
{
XmlAttribute showname = node.Attributes["showname"];
XmlAttribute show = node.Attributes["show"];
XmlAttribute value = node.Attributes["value"];
if (showname != null || show != null || value != null)
{
Console.Write("tagname:{0} ", node.Name);
if (showname != null)
Console.Write("showname:{0} ", showname.Value);
if (show != null)
Console.Write("show:{0} ", show.Value);
if (value != null)
Console.Write("value:{0} ", value.Value);
Console.WriteLine();
}
foreach (XmlNode xn in node.ChildNodes)
GetNodes(xn);
}
Eric290 2009-04-02
  • 打赏
  • 举报
回复
我不要粘贴复制...
zzxap 2009-04-02
  • 打赏
  • 举报
回复
[code=C#]
读:
//打开某文件(假设web.config在根目录中)
string filename=Server.MapPath("/") + @"WebApplication1\web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);

//得到顶层节点列表
XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()=="appsettings")
{

//得到该节点的子节点
XmlNodeList nodelist=element.ChildNodes;

if ( nodelist.Count >0 )
{
//DropDownList1.Items.Clear();

foreach(XmlElement el in nodelist)//读元素值
{
//DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
//this.TextBox2.Text=el.Attributes["key"].InnerText;
this.TextBox2.Text=el.Attributes["key"].Value;
this.Label1.Text=el.Attributes["value"].Value;


//同样在这里可以修改元素值,在后面save。
// el.Attributes["value"].Value=this.TextBox2.Text;
}


}

}

}

xmldoc.Save(filename);

}

效果:
<appSettings>
<add key="密码" value="admin" />
<add key="ltp" value="first" />
</appSettings>

//添加空元素:
XmlNode node=doc.CreateElement(groupname);
node.InnerText="";
doc.LastChild.AppendChild(node);

doc.Save(xmlfile);

删除一个节点元素
string itemname=this.listBox1.SelectedItem.ToString();

this.listBox1.Items.Remove(this.listBox1.SelectedItem);

//begin del xmlfile
XmlDocument doc=new XmlDocument();
doc.Load(xmlfile);

XmlNodeList topM=doc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name==this.comboBox1.Text)
{

//得到该节点的子节点
XmlNodeList nodelist=element.ChildNodes;

foreach(XmlElement el in nodelist)//读元素值
{
if(el.Attributes["key"].Value==itemname)

{
element.RemoveChild(el);
}

}//循环元素

}//得到组

}//循环组

doc.Save(xmlfile); //一定要保存一下,否则不起作用

//筛选数据
private void Reader_Xml(string pathFlie)
{
XmlDocument Xmldoc=new XmlDocument();
Xmldoc.Load(pathFlie);
XmlNodeList Record1=Xmldoc.DocumentElement.SelectNodes(Code[@id='1'])
int f=0;
foreach(XmlNode xnode in Record1)
{

}
} /**//*读取xml数据 两种xml方式*/
<aaa>
<bb>something</bb>
<cc>something</cc>
</aaa>

<aaa>
<add key="123" value="321"/>
</aaa>

/**//*第一种方法*/
DS.ReadXml("your xmlfile name");
Container.DataItem("bb");
Container.DataItem("cc");
DS.ReadXmlSchema("your xmlfile name");

/**//*第二种方法*/
<aaa>
<add key="123" value="321"/>
</aaa>
如果我要找到123然后取到321应该怎么写呢?

using System.XML;
XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
xmlDoc.Load(@"c:\Config.xml");
XmlElement elem = xmlDoc.GetElementById("add");
string str = elem.Attributes["value"].Value


/**//*第三种方法: SelectSingleNode 读取两种格式的xml *---/
--------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString>


在某节点下增加一个元素,并设置值:

if(element.Name.ToLower()=="appsettings")
{

XmlElement elem =xmldoc.CreateElement("add");

element.AppendChild(elem);
elem.InnerText="ltp";

xmldoc.Save(filename);

}

效果:
<appSettings>
<add key="密码" value="admin" />
<add>ltp</add>
</appSettings>

在某节点下增加一个元素,并增加两个属性:
if(element.Name.ToLower()=="appsettings")
{

XmlElement elem =xmldoc.CreateElement("add");
element.AppendChild(elem);

XmlAttribute xa=xmldoc.CreateAttribute("key");
xa.Value="ltp";

XmlAttribute xa2=xmldoc.CreateAttribute("value");
xa2.Value="first";

elem.SetAttributeNode(xa);
elem.SetAttributeNode(xa2);


xmldoc.Save(filename);

</appSettings>
</configuration>
--------------------------------------------------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(strXmlName);

XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");
if(node!=null)
{
string k1=node.Value; //null
string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
node=null;
}

********************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" />
</appSettings>
</configuration>
**--------------------------------------------------------------------**
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
string k=node.Attributes["key"].Value;
string v=node.Attributes["value"].Value;
node=null;
}
*--------------------------------------------------------------------*
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
XmlNodeReader nr=new XmlNodeReader(node);
nr.MoveToContent();
//检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
nr.MoveToAttribute("value");
string s=nr.Value;
node=null;
}
[/CODE]

110,534

社区成员

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

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

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