使用C#读取XML数据 [英] Read XML data using C#
问题描述
大家好,
我有以下格式的XML。我需要从节点名称获取first,last,id值的数据。你能帮我解决这个问题吗?
Hi All,
I am having an XML in the following format. Which i need to get data of first, last, id values from the node name. Can you help how i can get this ?
<sectionIndex>
<sectionRef first="A00" last="A09" id="A00-A09">
Intestinal infectious diseases
</sectionRef>
<sectionRef first="A15" last="A19" id="A15-A19">
Tuberculosis
</sectionRef>
<sectionRef first="A20" last="A28" id="A20-A28">
Certain zoonotic bacterial diseases
</sectionRef>
<sectionRef first="A30" last="A49" id="A30-A49">
Other bacterial diseases
</sectionRef>
<sectionRef first="A50" last="A64" id="A50-A64">
Infections with a predominantly sexual mode of transmission
</sectionRef>
<sectionRef first="A65" last="A69" id="A65-A69">
Other spirochetal diseases
</sectionRef>
</sectionIndex>
谢谢。
我尝试过:
System.Xml.XmlNodeList章节= doc.SelectNodes(ICD10CM.tabular / chapter);
System.Xml.XmlNodeList Sections = doc.SelectNodes(ICD10CM.tabular / chapter / sectionIndex);
foreach(章节中的System.Xml.XmlNode CC)
{
string [] sections = new string [5];
sections [0] = CC.SelectSingleNode(sectionRef)。InnerText;
}
Thanks.
What I have tried:
System.Xml.XmlNodeList Chapters = doc.SelectNodes("ICD10CM.tabular/chapter");
System.Xml.XmlNodeList Sections = doc.SelectNodes("ICD10CM.tabular/chapter/sectionIndex");
foreach (System.Xml.XmlNode CC in Sections)
{
string[] sections = new string[5];
sections[0] = CC.SelectSingleNode("sectionRef").InnerText;
}
推荐答案
移动定义循环外的部分
,并使用索引来访问它。或者更好的是,使用列表< string>
,这样您就不必使用固定数量的元素:
Move the definition ofsections
outside the loop, and use an index to access it. Or better, use aList<string>
so you don't have to work with a fixed number of elements:
System.Xml.XmlNodeList Chapters = doc.SelectNodes("ICD10CM.tabular/chapter");
System.Xml.XmlNodeList Sections = doc.SelectNodes("ICD10CM.tabular/chapter/sectionIndex");
List<string> sections = new List<string>();
foreach (System.Xml.XmlNode CC in Sections)
{
sections.Add(CC.SelectSingleNode("sectionRef").InnerText);
}
这篇关于使用C#读取XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!