.net/c#如何解析.xml文件

快乐的萌汉子 2012-01-18 01:55:46
RT,现在有一个XML文件,在后台该如何解析。本人对XML不太了解,希望在次得到帮助,先谢谢了。每个XML文件里面的内容都不一样,现在求一个公用的方法,xml文件标准如下:

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Person IDCard="610424199902230099" Name="小田雨" MedicalID="体检编号" Sex="男" Age="22" MedicalRecordDate ="2011-01-01" MedicalReportDate="2011-01-01"
MedicalCount="体检次数" HospitalID="001" HospitalName="兴隆园医院" >
<Results>/* 病种(区别不出来病种和结论,则为空,只填结论) */
<Result></Result> /*多个疾病值*/
<Result></Result>
<Result></Result>
</Results>
<Conclusions> /*医师结论*/
<Conclusion></Conclusion>/*多个结论*/
<Conclusion></Conclusion>
<Conclusion></Conclusion>
</Conclusions>
<Suggestions> /*医师建议*/
<Suggestion></Suggestion>/*多个建议*/
<Suggestion></Suggestion>
<Suggestion></Suggestion>
</Suggestions>
<Health> 为空(预留)</Health>
</Person>

<MedicalItems>
<MedicalSub ID ="0001" Name="化学检查" >
<MedicalType ID ="0001001" Name="血常规" MedicalDoc="体检医师名字" MedicalDate="2011-02-13">

<Item ID="000100010001" Name="白细胞" Unit="G/L" Parameters="3.7--10.0" >
<Results>H==高,L=低,N=正常</Results>
<Value>11.1</Value>
<Disease></Disease>
<MedicalBodyPart> </MedicalBodyPart>
<MedicalImage> </MedicalImage>
<Conclusion ></Conclusion>
</Item>
<Item ID="000100010002" Name="红细胞" Unit="G/L" Parameters="3.7--10.0">
<Results>H==高,L=低,N=正常</Results>
<Value>11.1</Value>
<Disease></Disease>
<MedicalBodyPart> </MedicalBodyPart>
<MedicalImage> </MedicalImage>
<Conclusion ></Conclusion>
</Item>
</MedicalType>
</MedicalSub>
<MedicalSub ID ="0002" Name="物理检查" >
<MedicalType ID ="0002001" Name="B超" MedicalDoc="体检医师名字" MedicalDate="2011-02-13">
<Item ID="000200010001" Name="胸部B超" Unit=" " Parameters="">
<Results>A=异常,N=正常</Results>
<Value></Value>
<Disease>病种,未见异常</Disease>
<MedicalBodyPart>检查部位:胸部</MedicalBodyPart>
<MedicalImage>影像所见</MedicalImage>
<Conclusion >检查结论</Conclusion>
</Item>
<Item ID="000200010002" Name="腹部B超" Unit=" " Parameters="">
<Results>A=异常,N=正常</Results>
<Value></Value>
<Disease>病种,未见异常</Disease>
<MedicalBodyPart>检查部位:腹部</MedicalBodyPart>
<MedicalImage>影像所见</MedicalImage>
<Conclusion >检查结论</Conclusion>
</Item>
</MedicalType>

</MedicalSub>
<MedicalSub ID ="0005" Name="五官科" >
<MedicalType ID ="0005001" Name="眼科" MedicalDoc="体检医师名字" MedicalDate="2011-02-13">
<Item ID="000500010001" Name="视力/右" Unit=" " Parameters="1.0-1.5">
<Results>A=异常,N=正常</Results>
<Value>1.5</Value>
<Disease>病种,未见异常</Disease>
<MedicalBodyPart>检查部位</MedicalBodyPart>
<MedicalImage>影像所见</MedicalImage>
<Conclusion >检查结论</Conclusion>
</Item>
<Item ID="000500010002" Name="矫正视力/右" Unit=" " Parameters="1.0-1.5">
<Results>A=异常,N=正常</Results>
<Value>0.8</Value>
<Disease>病种,未见异常</Disease>
<MedicalBodyPart>检查部位</MedicalBodyPart>
<MedicalImage>影像所见</MedicalImage>
<Conclusion >检查结论</Conclusion>
</Item>
</MedicalType>

</MedicalSub>
</MedicalItems>
</Root>
...全文
476 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuweihug 2014-05-07
  • 打赏
  • 举报
回复
C# Linq使用XDocument读取Xml文件并形成结构树数据(json) http://www.suchso.com/projecteactual/Csharp-Linq-XDocument-Xml-File-Tree-Data.html
EnForGrass 2012-01-18
  • 打赏
  • 举报
回复

#region 显示XML数据
private void ReadXML()
{
string url = Server.MapPath("user.xml");//获得当前文件夹下的XML文件
StreamReader sRead = new StreamReader(url,System.Text.Encoding.GetEncoding("GB2312"));//以一种特定的编码从字节流读取字符,必须要转化成GB2312读取才不能出乱码
XmlDataDocument datadoc = new XmlDataDocument();//操作XML文档
datadoc.DataSet.ReadXml(sRead);//将读取的字节流存到DataSet里面去
this.DataGrid1.DataSource = datadoc.DataSet.Tables[0].DefaultView;
DataGrid1.DataKeyField="username";//以username建立索引
this.DataGrid1.DataBind();
datadoc = null;//清空对XML数据的操作
sRead.Close();//关闭字节流的读取
}
#endregion


public class XmlControl
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();

public XmlControl(string XmlFile)
{
//
// TODO: 在这里加入建构函式的程序代码
//
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}

public DataView GetData(string XmlPathNode)
{
//查找数据。返回一个DataView
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds.Tables[0].DefaultView;
}

public void Replace(string XmlPathNode,string Content)
{
//更新节点内容。
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}

public void Delete(string Node)
{
//删除一个节点。
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}

public void InsertNode(string MainNode,string ChildNode,string Element,string Content)
{
//插入一节点和此节点的一子节点。
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
}

public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content)
{
//插入一个节点,带一属性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib,AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}

public void InsertElement(string MainNode,string Element,string Content)
{
//插入一个节点,不带属性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}

public void Save()
{
//保存文檔。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
}

=========================================================

实例应用:

string strXmlFile = Server.MapPath("TestXml.xml");
XmlControl xmlTool = new XmlControl(strXmlFile);

// 数据显视
// dgList.DataSource = xmlTool.GetData("Book/Authors[ISBN=\"0002\"]");
// dgList.DataBind();

// 更新元素内容
// xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
// xmlTool.Save();

// 添加一个新节点
// xmlTool.InsertNode("Book","Author","ISBN","0004");
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
// xmlTool.Save();

// 删除一个指定节点的所有内容和属性
// xmlTool.Delete("Book/Author[ISBN=\"0004\"]");
// xmlTool.Save();

// 删除一个指定节点的子节点
// xmlTool.Delete("Book/Authors[ISBN=\"0003\"]");
// xmlTool.Save();

快乐的萌汉子 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zhuguibiao 的回复:]
先查,后插
[/Quote]
麻烦说具体点,我对这个了解不多。
古龙老子 2012-01-18
  • 打赏
  • 举报
回复
先查,后插
快乐的萌汉子 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 shancheng44 的回复:]
C# code

XmlDocument xmlDoc= new XmlDocument();
xmlDoc.LoadXml("xml文件路径");
XmlNodeList itemList = xmlDoc.SelectNodes("xml//Root//Person ");
foreach (XmlNode node in itemList)
……
[/Quote]

有点丝路了,但是就是不知道会不会写,我先试下。谢谢了。
快乐的萌汉子 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ohkuy 的回复:]
你想提取什么内容呢,一般都是按节点提取内容的
[/Quote]

里面的内容,只要有的,就全要。
快乐的萌汉子 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hailang466 的回复:]
简单的都用dataset 不过看你的结构有点复杂不知道 dataset 能hold住不,估计你还是要看文档啊
提供个用dataset的简单使用
dataset ds = new dataset();
ds.readxml("这是xml的路径");

然后xml中的数据就会在ds.tables里了 你自己研究下吧
[/Quote]

我开始就是用的dataset,但是里面解析出来的内容不全。
shancheng44 2012-01-18
  • 打赏
  • 举报
回复

XmlDocument xmlDoc= new XmlDocument();
xmlDoc.LoadXml("xml文件路径");
XmlNodeList itemList = xmlDoc.SelectNodes("xml//Root//Person ");
foreach (XmlNode node in itemList)
{
string idCard = node.Attributes["IDCard"].Value;
//....
//....
//....
}


再不会,哥也没招了
代码日志 2012-01-18
  • 打赏
  • 举报
回复
XmlDocument treedoc = new XmlDocument();
treedoc.LoadXml(xml内容);
shancheng44 2012-01-18
  • 打赏
  • 举报
回复

<?xml version="1.0"?>
<Root>
<CPU>BFEBFBFF0001067A</CPU>
<DATE>2012-1-8 17:24:33</DATE>
</Root>


这是XML文件内容,很简单
shancheng44 2012-01-18
  • 打赏
  • 举报
回复

CN_UTILITY.ComputerSystem csys = new ComputerSystem();
string sAllUserAppDataPath = csys.GetFolderPath();//获取系统AllUser下Application Data文件夹目录

string sWLApplication = sAllUserAppDataPath + "\\JiuJiuTong";
string sWlTraFile = sWLApplication + "\\***.tra";

if (!Directory.Exists(sWLApplication))//判断目录是否存在
{
Directory.CreateDirectory(sWLApplication);
}
XmlDocument doc = new XmlDocument();
if (!File.Exists(sWlTraFile))//文件不存在
{
string sCpuId = csys.GetCpuId();//获取电脑CPU编号

XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
doc.AppendChild(xmlnode);//加入XML的声明段落
XmlElement Root = doc.CreateElement("Root");
XmlElement E_CPU = doc.CreateElement("CPU");
XmlElement E_DATE = doc.CreateElement("DATE");
E_CPU.InnerText = sCpuId;
E_DATE.InnerText = DateTime.Now.ToString();
Root.AppendChild(E_CPU);
Root.AppendChild(E_DATE);
doc.AppendChild(Root);
doc.Save(sWlTraFile);

return true;
}
else
{
doc.Load(sWlTraFile);//打开指定文件
XmlNode cpu = doc.SelectSingleNode("Root//CPU"); XmlNode date = doc.SelectSingleNode("Root//DATE");
if (cpu != null && date != null)
{
if (DateTime.Compare(DateTime.Now, Convert.ToDateTime(date.InnerText).AddHours(24)) < 0)
{
return true;
}
}
return false;
}


看这段解析XML代码,希望对于你有所帮助
ohkuy 2012-01-18
  • 打赏
  • 举报
回复
你想提取什么内容呢,一般都是按节点提取内容的
hailang466 2012-01-18
  • 打赏
  • 举报
回复
简单的都用dataset 不过看你的结构有点复杂不知道 dataset 能hold住不,估计你还是要看文档啊
提供个用dataset的简单使用
dataset ds = new dataset();
ds.readxml("这是xml的路径");

然后xml中的数据就会在ds.tables里了 你自己研究下吧
快乐的萌汉子 2012-01-18
  • 打赏
  • 举报
回复
顶一下,没有好心人帮忙吗?
快乐的萌汉子 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
xmldocument,看帮助
[/Quote]
看了半天,也没看怎么明白,现在比较急,希望好心人帮忙。
bdmh 2012-01-18
  • 打赏
  • 举报
回复
xmldocument,看帮助

110,524

社区成员

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

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

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