急!!xml的问题

chenyu001 2004-04-16 09:47:13
xml文件:
<?xml version="1.0" encoding="GB2312"?>
<ModuleItem>
<Item id="1" name="人力资源管理">
<SubItem id="10" name="人务资源计划" ico="" href=""></SubItem>
<SubItem id="11" name="招聘与选才" ico="" href=""></SubItem>
<SubItem id="12" name="人事信息管理" ico="" href=""></SubItem>
<SubItem id="13" name="考勤与休假" ico="" href=""></SubItem>
<SubItem id="14" name="绩效管理" ico="" href=""></SubItem>
<SubItem id="15" name="薪酬福利" ico="" href=""></SubItem>
<SubItem id="16" name="就餐管理" ico="" href=""></SubItem>
<SubItem id="17" name="培训发展" ico="" href=""></SubItem>
<SubItem id="18" name="离职退休" ico="" href=""></SubItem>
</Item>
</ModuleItem>

用C# 根据没一个ID检索出它的子项,然后存放到数组中,谢谢各位大哥帮忙!
...全文
63 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
CMIC 2004-04-16
  • 打赏
  • 举报
回复
private string[] ReStrs(string sID)
{
XmlDocument mXml=new XmlDocument();
mXml.Load(@"D:\\q.xml");
XmlNodeList mNodes=mXml.SelectNodes("//SubItem");
foreach(XmlNode sNode in mNodes)
{
if(sNode.Attributes["id"].InnerText==sID)
{
string[] mStrs=new string[sNode.Attributes.Count];
for(int i=0;i<sNode.Attributes.Count;i++)
{
mStrs[i]=sNode.Attributes[i].InnerText;
}
return mStrs;
}
}
return new string[1]{""};
}
marvelstack 2004-04-16
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;


namespace ZZ
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string strXml="<?xml version=\"1.0\"?>"
+"<Data>"
+"<Head>"
+"<Nodeid>1111</Nodeid>"
+"<Subid>2222</Subid>"
+"<Version>2004</Version>"
+"<Date>20040302</Date>"
+"<Time>101500</Time>"
+"</Head>"
+"<Body>"
+"<Code>01</Code>"
+"<Name>深圳</Name>"
+"<IdType>0</IdType>"
+"<Idno>110258740824082</Idno>"
+"</Body>"
+"</Data>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
string vv;

XmlNodeList myNodes = doc.GetElementsByTagName("Version");
vv = myNodes[0].InnerText;
//修改
myNodes[0].InnerText = "123455";
Console.WriteLine(vv);
myNodes = doc.SelectNodes("//Version")[0].InnerText;
vv = myNodes[0].InnerText;
Console.WriteLine(vv);
//再修改
myNodes[0].InnerText = "67890";
doc.Save("d:\\text.xml");
Console.ReadLine();
}

}

}
CMIC 2004-04-16
  • 打赏
  • 举报
回复
private string[] ReStrs()
{
XmlDocument mXml=new XmlDocument();
mXml.Load(@"D:\\q.xml");
//XmlNodeList mNodes=mXml.SelectNodes("//SubItem");
XmlNode mNode=mXml.SelectSingleNode("//Item[@id=1]");
string[] mStrs=new string[mNode.ChildNodes.Count];
for(int i=0;i<mNode.ChildNodes.Count;i++)
{
// if(sNode.Attributes["id"].InnerText==sID)
// {
// string[] mStrs=new string[sNode.Attributes.Count];
// for(int i=0;i<sNode.Attributes.Count;i++)
// {
// mStrs[i]=sNode.Attributes[i].InnerText;
// }
// return mStrs;
// }
mStrs[i]=mNode.ChildNodes[i].OuterXml;

}
return mStrs;


}
chenyu001 2004-04-16
  • 打赏
  • 举报
回复
再问 怎么取得Item id="1" name="人力资源管理 我要的是根据Item 检索SubItem

谢谢各位了
liduke 2004-04-16
  • 打赏
  • 举报
回复
也可以
System.IO.FileStream fs = new System.IO.FileStream(@"C:\Inetpub\wwwroot\Test\XMLFile1.xml",System.IO.FileMode.Open);
System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(fs);
ds = new System.Data.DataSet();
ds.ReadXml(xr);
this._question = ds.Tables[0].Rows[0][1].ToString();
this._votecount = ds.Tables[1].Rows.Count;

this._answers = new string[_votecount];
this._votes = new int[_votecount];
for(int i= 0 ;i<this._votecount;i++)
{
this._answers[i] = ds.Tables[1].Rows[i][0].ToString();
this._votes[i] = int.Parse(ds.Tables[1].Rows[i][1].ToString());
}
xr.Close();
fs.Close();
Maconel 2004-04-16
  • 打赏
  • 举报
回复
搞定了吗
taofirst 2004-04-16
  • 打赏
  • 举报
回复
看看
chenyu001 2004-04-16
  • 打赏
  • 举报
回复
比如说 SID是什么意思

谢谢你了

111,097

社区成员

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

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

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