日期:2014-05-19  浏览次数:20839 次

C# 操作XML的问题
一方法返回XML文件节点的DataView
如DataView   view   =   GetData(string   xpath);
可以通过view[0][ "attrib1 "]获取属性attrib1的值

问题:如何获取 <label> 标签中的内部文本值?即 "文本文本... "

XML文件结构
<labels>
        <label   name= "A "   attrib1= " "   >
        文本文本...
        </label>
</labels>

------解决方案--------------------
innerText
------解决方案--------------------
xpath = "labels/label ";
------解决方案--------------------
不好意思,上面的有问题。
------解决方案--------------------
你先遍历一次属性看看
------解决方案--------------------
//xml
string xml = @ " <lables>
<label name= " "A " " attrib1= " "att1 " "> text </label>
<label name= " "B " " attrib1= " "att2 " "> 中文 </label>
</lables>
";
//读入内存流
MemoryStream ms=new MemoryStream(Encoding.UTF8.GetBytes(xml));
//载入到dataset
DataSet ds = new DataSet();
ds.ReadXml(ms);
//取默认视图
DataView dv = ds.Tables[0].DefaultView ;
//遍历行
foreach (DataRowView drv in dv) {
//遍历列
foreach (DataColumn dc in dv.Table.Columns) {
//输出
Console.Write( "{0}:{1}\t ", dc.Caption, drv[dc.Caption]);
}
Console.WriteLine();
}
Console.Read();


=========运行结果
name:A attrib1:att1 label_Text:text
name:B attrib1:att2 label_Text:中文...

------解决方案--------------------
可见以节点名加_Text可以取得innerText属性