注 我用的是C#我自己写了一个类,功能是读取XML文件中的指定节点的值。
XML文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<Patients>
<Patients_virtue>
<ID>001</ID>
<Last_Name>jay</Last_Name>
<First_Name>hoho</First_Name>
<MI>0091</MI>
<Creation_Date>20091220</Creation_Date>
<Patient_ID>100</Patient_ID>
<Sex>nan</Sex>
<Date_of_Birth>19820611</Date_of_Birth>
<Patient_Photo>C:Patient_Photo\ID001.png</Patient_Photo>
<Weight>75kg</Weight>
<Known_Allergies>yes</Known_Allergies>
<Specialty>ewet3</Specialty>
<MRN>1.5</MRN>
<Purpose_of_Consultation>edawfvds</Purpose_of_Consultation>
</Patients_virtue>
</Patients>类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;namespace ClassLibrary1
{
public class SiteC
{
//Defines a common attribute syntagma
struct Common
{
public string Last_Name;
public string First_Name;
public string MI;
public string Creation_Date;
public string Patient_ID;
} //Defines a patient info the syntagma
struct Patient_Info
{
public string Patient_Photo;
public string Weight;
public string Known_Allergies;
public string Medical_History;
public string Preliminary_Diagnosis;
public string Purpose_of_Consultation;
} public void Read_XML(string Patient_ID)
{
}
}
}是根据 ID 从XML文件中取得 Last_Name,First_Name,MI ...这些节点的值 赋给 结构体变量 Last_Name,First_Name,MI ...
请高手们指教一下,小弟初学是在不知道咋用 XmlDocument
请给出详细代码 写的越易懂越好。小弟在此谢过了。。答案好的话,还会给追加分值的,谢谢了
我就是要读取节点的值 比如 得到 Last_Name 的值 jay 赋给我的 成员变量 Last_Name 怎么得到?
XML文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<Patients>
<Patients_virtue>
<ID>001</ID>
<Last_Name>jay</Last_Name>
<First_Name>hoho</First_Name>
<MI>0091</MI>
<Creation_Date>20091220</Creation_Date>
<Patient_ID>100</Patient_ID>
<Sex>nan</Sex>
<Date_of_Birth>19820611</Date_of_Birth>
<Patient_Photo>C:Patient_Photo\ID001.png</Patient_Photo>
<Weight>75kg</Weight>
<Known_Allergies>yes</Known_Allergies>
<Specialty>ewet3</Specialty>
<MRN>1.5</MRN>
<Purpose_of_Consultation>edawfvds</Purpose_of_Consultation>
</Patients_virtue>
</Patients>类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;namespace ClassLibrary1
{
public class SiteC
{
//Defines a common attribute syntagma
struct Common
{
public string Last_Name;
public string First_Name;
public string MI;
public string Creation_Date;
public string Patient_ID;
} //Defines a patient info the syntagma
struct Patient_Info
{
public string Patient_Photo;
public string Weight;
public string Known_Allergies;
public string Medical_History;
public string Preliminary_Diagnosis;
public string Purpose_of_Consultation;
} public void Read_XML(string Patient_ID)
{
}
}
}是根据 ID 从XML文件中取得 Last_Name,First_Name,MI ...这些节点的值 赋给 结构体变量 Last_Name,First_Name,MI ...
请高手们指教一下,小弟初学是在不知道咋用 XmlDocument
请给出详细代码 写的越易懂越好。小弟在此谢过了。。答案好的话,还会给追加分值的,谢谢了
我就是要读取节点的值 比如 得到 Last_Name 的值 jay 赋给我的 成员变量 Last_Name 怎么得到?
doc.load("xxx.xml");
xmlnode node=doc.docmentelement.selectsinglenode("/Patients/Patients_virtue/Last_Name");
string lastname=node.innertext;
node=doc.docmentelement.selectsinglenode("("/Patients/Patients_virtue/First_Name");
string firstname=node.inner.text;
<?xml version="1.0" encoding="utf-8"?>
<Patients>
<Patients_virtue ID = "001">
<Last_Name>jay</Last_Name>
<First_Name>hoho</First_Name>
<MI>0091</MI>
<Creation_Date>20091220</Creation_Date>
<Patient_ID>100</Patient_ID>
</Patients_virtue>
<Patients_virtue ID = "002">
<Last_Name>LiLi</Last_Name>
<First_Name>ccc</First_Name>
<MI>1015</MI>
<Creation_Date>20091221</Creation_Date>
<Patient_ID>101</Patient_ID>
</Patients_virtue>
<Patients_virtue ID = "003">
<Last_Name>suzuki</Last_Name>
<First_Name>tanaka</First_Name>
<MI>1231</MI>
<Creation_Date>20091222</Creation_Date>
<Patient_ID>110</Patient_ID>
</Patients_virtue>
</Patients>就是根据 ID 分别取 ID =“001” ,ID =“002”,ID =“003”的:Last_Name,First_Name 怎么去取得 谢谢了
document.Load(@"aa.xml");
XmlNode basicNode = document.DocumentElement;
foreach (XmlNode node in basicNode.ChildNodes)
{
//在这里判断
if (node.Attributes["ID"].Value.ToString().Trim() == "001")
{
foreach (XmlNode pnode in node.ChildNodes)
{
switch (pnode.Name)
{
//InnerText取值
case "Last_Name":
string lastname = pnode.InnerText;
break; default:
break;
}
}
}
}
doc.load("xxx.xml");
xmlnode node=doc.docmentelement.selectsinglenode("/Patients/Patients_virtue/Last_Name");
string lastname=node.innertext;
node=doc.docmentelement.selectsinglenode("/Patients/Patients_virtue["ID=['01']")
string firstname=node.childnodes[1];
string lastname=node.childnodes[0]
if(node!=null)
{
firstname=node.childnodes[1].innertext;
lastname=node.childnodes[0].innertext;}
直接定位效率高,楼上的遍历方法可行,不好
XmlElement ele = xml.DocumentElement; XmlNodeList xml1 = ele.ChildNodes[id - 1].ChildNodes; return xml1[0].ChildNodes[0].InnerText.ToString();//读取指定属性值的Node } //读取指定属性值的Node的total protected string GetTotal(int id,string xmlPath) { XmlDocument xml = new XmlDocument(); xml.Load(xmlPath);
XmlElement ele = xml.DocumentElement; XmlNodeList xmlList = ele.ChildNodes[id - 1].ChildNodes;
return xmlList[1].ChildNodes[0].InnerText.ToString(); }
能根据我的 xml 文件给我 写一个 效率高的代码么 注释的清楚些 每一行代表什么 写一下 谢谢了
我想给据ID 去找xml里面的 任意一个节点的值。。
我太新手了 楼上都都说学到东西了 我还是晕晕忽忽的 我咋这么笨呢 帮帮我吧
XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。下面列出了最有用的路径表达式:
表达式 描述
nodename 选取此节点的所有子节点
/ 从根节点选取
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
. 选取当前节点
.. 选取当前节点的父节点
@ 选取属性 实例
在下面的表格中,我们已列出了一些路径表达式以及表达式的结果:路径表达式 结果
bookstore 选取 bookstore 元素的所有子节点
/bookstore 选取根元素 bookstore注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!
bookstore/book 选取所有属于 bookstore 的子元素的 book 元素。
//book 选取所有 book 子元素,而不管它们在文档中的位置。
bookstore//book 选择所有属于 bookstore 元素的后代的 book 元素,而不管它们位于 bookstore 之下的什么位置。
//@lang 选取所有名为 lang 的属性。
谓语(Predicates)
谓语用来查找某个特定的节点或者包含某个指定的值的节点。谓语被嵌在方括号中。实例
在下面的表格中,我们列出了带有谓语的一些路径表达式,以及表达式的结果:路径表达式 结果
/bookstore/book[1] 选取属于 bookstore 子元素的第一个 book 元素。
/bookstore/book[last()] 选取属于 bookstore 子元素的最后一个 book 元素。
/bookstore/book[last()-1] 选取属于 bookstore 子元素的倒数第二个 book 元素。
/bookstore/book[position()<3] 选取最前面的两个属于 bookstore 元素的子元素的 book 元素。
//title[@lang] 选取所有拥有名为 lang 的属性的 title 元素。
//title[@lang='eng'] 选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。
/bookstore/book[price>35.00] 选取所有 bookstore 元素的 book 元素,且其中的 price 元素的值须大于 35.00。
/bookstore/book[price>35.00]/title 选取所有 bookstore 元素中的 book 元素的 title 元素,且其中的 price 元素的值须大于 35.00。
选取未知节点
XPath 通配符可用来选取未知的 XML 元素。通配符 描述
* 匹配任何元素节点
@* 匹配任何属性节点
node() 匹配任何类型的节点 实例
在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:路径表达式 结果
/bookstore/* 选取 bookstore 元素的所有子节点
//* 选取文档中的所有元素
//title[@*] 选取所有带有属性的 title 元素。
选取若干路径
通过在路径表达式中使用“|”运算符,您可以选取若干个路径。实例
在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:路径表达式 结果
//book/title | //book/price 选取所有 book 元素的 title 和 price 元素。
//title | //price 选取所有文档中的 title 和 price 元素。
/bookstore/book/title | //price 选取所有属于 bookstore 元素的 book 元素的 title 元素,以及文档中所有的 price 元素。
xmldocment doc=new xmldocment();
doc.load("xxx.xml");
xmlnode node=doc.docmentelement.selectsinglenode("//Patients/Patients_virtue[@ID=\"001\"]");
if(node.children.cout>2)
{
_LastName=node.children[0].innerText.value;
_FirstName=node.children[1].innerText.value;
}
doc.Load("D:/vs2008/WorkSpace/XML/XMLFile.xml");
XmlNodeList node = doc.SelectNodes("custom/user");
foreach (XmlNode singlenode in node)
{
if (singlenode.InnerXml == "name")
{
labName.Text = singlenode.InnerText;
}
else
{
labPassword.Text = singlenode.InnerText;
}
}
XML <custom>
<user>
<name>哈哈</name>
<password>密码</password>
</user>
</custom>页面:
<table>
<tr><td>
<asp:Label ID="labName" runat="server" Text=""></asp:Label></td></tr>
<tr><td>
<asp:Label ID="Label1" runat="server" Text="123123123"></asp:Label></td></tr>
<tr><td>
<asp:Label ID="labPassword" runat="server" Text=""></asp:Label></td></tr>
</table>显示: 123123123
哈哈密码
为什么显示的结果"哈哈密码"中没有换行