2017-02-10 23 views
0

我有下面的序列化的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标签的值,但没有其他值。如何解决这个问题。

在此先感谢。

+0

你是什么意思_No其他VALUE_? –

+0

有很多带有“属性”的标签,我想提取所有标签 – Varun

+0

您是否有使用XmlDocument的限制?你可以XDocument? – KernelMode

回答

0

试试这个

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>"; 
XDocument doc = XDocument.Parse(s); 
var NodeNames = doc.Descendants("DataItem").Elements(); 
foreach (var item in NodeNames) 
{ 
    string firstName = item.Value; 
    Console.WriteLine(firstName); 
} 
0

XDocumentXmlDocument更新。更可读,你可以使用LINQ。

var xElements = XDocument.Parse(yourXml).Descendants("Property"); 
xElements.ToList().ForEach(x=> Console.WriteLine(x.Value)); 
0

您可以使用LINQ到XML:

var results = from node in XDocument.Parse(s).Descendants() 
       where node.Name == "Property" 
       select node.Value; 

结果: enter image description here

0

试试这个代码:

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=\"64ced8f3-385a-238c-eea4-008bab8ba249\"><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\">Microsoft.EnterpriseManagement.Mom.Modules.NetmonDataSource.NetmonDataSource</Property><Property Name=\"StackTrace\" VariantType=\"8\">System.Exception: [2/3/2017 9:50:29 AM][InitializeDataReceiver]Exception while trying to connect to the agent :Could not open pipe, CreateFile Error : 2 WaitNamedPipe Error : 2 Pipe guid is : d0c4c51e-543b-4f25-8453-40000066967d</Property></DataItem>"; 
     XmlDocument xml = new XmlDocument(); 
     xml.LoadXml(s); 
     XmlNodeList xnList = xml.SelectNodes("/DataItem/Property"); 
     foreach (XmlNode xn in xnList) 
     { 
      string firstName = xn.InnerText; 
      Console.WriteLine(firstName); 
     } 
    } 
} 
0

试试这个代码:

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) 
{ 
    XmlNodeList propsList = xn.SelectNodes("Property"); 
    foreach (XmlNode node in propsList) 
    { 
     string firstName = node.InnerText; 
     Console.WriteLine(firstName); 
    } 
} 
0

我喜欢用一本字典在这样的情况下使用XML LINQ创建词典:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 



namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      Dictionary<string, string> dict = doc.Descendants("Property") 
       .GroupBy(x => (string)x.Attribute("Name"), y => (string)y) 
       .ToDictionary(x => x.Key, y => y.FirstOrDefault()); 
     } 
    } 



}