我有一个字符串strXml 是xml格式:
strXml="
<ListConferenceResponse xmlns="http://mediax.huawei.com/webservice/types">
<code xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:int">200</code>
<description xmlns="">Success</description>
<page xmlns="">
<index>1</index>
<total>1</total>
<hasPrev>false</hasPrev>
<hasNext>false</hasNext>
<hasFirst>false</hasFirst>
<hasLast>false</hasLast>
<data>
<entry>
<key>StartTime</key>
<value>1274137920000</value>
</entry>
<entry>
<key>Subject</key>
<value>voice test</value>
</entry>
<entry>
<key>ConferenceID</key>
<value>1237954196</value>
</entry>
<entry>
<key>SubConferenceID</key>
<value>0</value>
</entry>
<entry>
<key>ConferenceState</key>
<value>Schedule</value>
</entry>
<entry>
<key>Length</key>
<value>121</value>
</entry>
<entry>
<key>organizationID</key>
<value>topOrganization</value>
</entry>
</data>
</page>
</ListConferenceResponse>
";如何获取里面所有的 key 和 value ,并且赋值给 List
strXml="
<ListConferenceResponse xmlns="http://mediax.huawei.com/webservice/types">
<code xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:int">200</code>
<description xmlns="">Success</description>
<page xmlns="">
<index>1</index>
<total>1</total>
<hasPrev>false</hasPrev>
<hasNext>false</hasNext>
<hasFirst>false</hasFirst>
<hasLast>false</hasLast>
<data>
<entry>
<key>StartTime</key>
<value>1274137920000</value>
</entry>
<entry>
<key>Subject</key>
<value>voice test</value>
</entry>
<entry>
<key>ConferenceID</key>
<value>1237954196</value>
</entry>
<entry>
<key>SubConferenceID</key>
<value>0</value>
</entry>
<entry>
<key>ConferenceState</key>
<value>Schedule</value>
</entry>
<entry>
<key>Length</key>
<value>121</value>
</entry>
<entry>
<key>organizationID</key>
<value>topOrganization</value>
</entry>
</data>
</page>
</ListConferenceResponse>
";如何获取里面所有的 key 和 value ,并且赋值给 List
using System.Text;
using System.Xml;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string strXml = "<ListConferenceResponse xmlns=\"http://mediax.huawei.com/webservice/types\"><code xmlns=\"\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:int\">200</code><description xmlns=\"\">Success</description><page xmlns=\"\"><index>1</index><total>1</total><hasPrev>false</hasPrev><hasNext>false</hasNext><hasFirst>false</hasFirst><hasLast>false</hasLast><data><entry><key>StartTime</key><value>1274137920000</value></entry><entry><key>Subject</key><value>voice test</value></entry><entry><key>ConferenceID</key><value>1237954196</value></entry><entry><key>SubConferenceID</key><value>0</value></entry><entry><key>ConferenceState</key><value>Schedule</value></entry><entry><key>Length</key><value>121</value></entry><entry><key>organizationID</key><value>topOrganization</value></entry></data></page></ListConferenceResponse>";
StringBuilder list = new StringBuilder(); XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strXml);
XmlNodeList lstNode = xmlDoc.SelectNodes("//entry");
foreach (XmlNode node in lstNode)
list.Append(node.SelectSingleNode("key").InnerText + "," + node.SelectSingleNode("value").InnerText + "||");
Console.WriteLine(list.ToString()); //Result:
//StartTime,1274137920000||Subject,voice test||ConferenceID,1237954196||SubConferenceID,0||ConferenceState,Schedule||Length,121||organizationID,topOrganization||
Console.Read(); }
}
}
doc.Load(Application.StartupPath + @"\xx.xml");
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (node.NodeType == XmlNodeType.Element)
{
foreach (XmlNode tn in node.ChildNodes)
{
//这里可以取到节点值了
if (tn.NodeType == XmlNodeType.Element)
{
string Name = tn.Attributes["name"].Value;
//其他操作
}
}
}
}