我有下面的序列化的XML:提取值
<DataItem type="System.PropertyBagData" time="2017-02-03T09:50:29.1118296Z" sourceHealthServiceId="">
<Property Name="LoggingComputer" VariantType="8">g2aaS03OsX/9e5SSikdrVjFb4tkwhVUWeGh6pOv8nJ0=</Property>
<Property Name="EventDisplayNumber" VariantType="8">4502</Property>
<Property Name="ManagementGroupName" VariantType="8">/FTyfF2bs7hBhlQMJfSABYkkuTU98A80WiXu9TlL98w=</Property>
<Property Name="RuleName" VariantType="8">CollectNetMonInformation</Property>
<Property Name="ModuleTypeName" VariantType="8"/>
<Property Name="StackTrace" VariantType="8">System.Exception: [2/3/2017 9:50:29 AM][InitializeDataReceiver], CreateFile Error : 2 WaitNamedPipe Error : 2 Pipe guid is : d0c4c51e-543b-4f25-8453-40000066967d</Property>
</DataItem>
要提取的 「屬性」 標籤的值,我寫了下面的C#代碼:
using System.IO;
using System;
using System.Xml;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
string s = "<DataItem type=\"System.PropertyBagData\" time=\"2017-02-03T09:50:29.1118296Z\" sourceHealthServiceId=\"\"><Property Name=\"LoggingComputer\" VariantType=\"8\">g2aaS03OsX/9e5SSikdrVjFb4tkwhVUWeGh6pOv8nJ0=</Property><Property Name=\"EventDisplayNumber\" VariantType=\"8\">4502</Property><Property Name=\"ManagementGroupName\" VariantType=\"8\">/=</Property><Property Name=\"RuleName\" VariantType=\"8\">CollectNetMonInformation</Property><Property Name=\"ModuleTypeName\" VariantType=\"8\"></Property><Property Name=\"StackTrace\" VariantType=\"8\">System.Exception: [2/3/2017 9:50:29 AM][InitializeDataReceiver]: 2 WaitNamedPipe Error : 2 Pipe guid is : </Property></DataItem>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(s);
XmlNodeList xnList = xml.SelectNodes("/DataItem");
foreach (XmlNode xn in xnList)
{
string firstName = xn["Property"].InnerText;
Console.WriteLine(firstName);
}
}
}
時我運行程序,我得到的輸出爲「g2aaS03OsX/9e5SSikdrVjFb4tkwhVUWeGh6pOv8nJ0 =」,這是第一個Property標籤的值,但沒有其他值。如何解決這個問題。
在此先感謝。
你是什麼意思_No其他VALUE_? –
有很多帶有「屬性」的標籤,我想提取所有標籤 – Varun
您是否有使用XmlDocument的限制?你可以XDocument? – KernelMode