请教如何取不同xml文件中标签里面的数据,如下代码所示
要求1是取出number="1"里面的1,2数据
要求2是取出<divisions></divisions>,<beats></beats>,<step></step>,<duration></duration>标签之间的数据--------------------------------------------------------------
<part id="P1">
<measure number="1">
<attributes>
<divisions>2</divisions>
<time>
<beats>4</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>F</step>
<octave>4</octave>
</pitch>
<duration>1</duration>
<voice>1</voice>
<type>eighth</type>
<stem>up</stem>
</note>
</measure>
<measure number="2">
<note>
<pitch>
<step>D</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>half</type>
<stem>up</stem>
</note>
<note>
<chord/>
<pitch>
<step>C</step>
<octave>5</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>half</type>
<stem>up</stem>
</note>
</measure>
<measure number="3">
<note>
<pitch>
<step>D</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>half</type>
<stem>up</stem>
<notations>
<articulations>
<staccato/>
</articulations>
</notations>
</note>
</measure>
</part>
要求1是取出number="1"里面的1,2数据
要求2是取出<divisions></divisions>,<beats></beats>,<step></step>,<duration></duration>标签之间的数据--------------------------------------------------------------
<part id="P1">
<measure number="1">
<attributes>
<divisions>2</divisions>
<time>
<beats>4</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>F</step>
<octave>4</octave>
</pitch>
<duration>1</duration>
<voice>1</voice>
<type>eighth</type>
<stem>up</stem>
</note>
</measure>
<measure number="2">
<note>
<pitch>
<step>D</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>half</type>
<stem>up</stem>
</note>
<note>
<chord/>
<pitch>
<step>C</step>
<octave>5</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>half</type>
<stem>up</stem>
</note>
</measure>
<measure number="3">
<note>
<pitch>
<step>D</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>half</type>
<stem>up</stem>
<notations>
<articulations>
<staccato/>
</articulations>
</notations>
</note>
</measure>
</part>
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\test.xml"); string path = "part/measure";
string refPaht = "";//相对路径
XmlNode tempNode = null; XmlNodeList nodelist= xmlDoc.SelectNodes(path);//查询所有这种路径的节点 foreach(XmlNode node in nodelist)
{
XmlAttribute attr = node.Attributes["number"];
string attrVal=attr.Value; //属性值 refPaht = "attributes/divisions";
tempNode=node.SelectSingleNode(refPaht); if(tempNode!=null)
{
string div = tempNode.InnerText; //divisions节点值
}
refPaht = "attributes//beats";
tempNode=node.SelectSingleNode(refPaht); if (tempNode != null)
{
string beats = tempNode.InnerText; //beats节点值
} refPaht = "note//step";
tempNode=node.SelectSingleNode(refPaht); if (tempNode != null)
{
string step = tempNode.InnerText; //step节点值
} refPaht = "note//duration";
tempNode=node.SelectSingleNode(refPaht); if (tempNode != null)
{
string duration = tempNode.InnerText; //duration节点值
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}