从C#中的序列化XML中提取值

时间:2017-02-10 09:03:16

标签: c# xml

我有以下序列化的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>

提取&#34;属性&#34;的值标签,我写了以下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);
       }
   }
}

当我运行程序时,我得到输出为&#34; g2aaS03OsX / 9e5SSikdrVjFb4tkwhVUWeGh6pOv8nJ0 =&#34;这是第一个Property标签的值,但没有其他值。如何解决这个问题。

提前致谢。

6 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

DateXDocument更新。更具可读性,您可以使用LINQ。

XmlDocument

答案 2 :(得分:0)

您可以使用Linq to XML:

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

结果: enter image description here

答案 3 :(得分: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);
        }
    }
}

答案 4 :(得分: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);
    }
}

答案 5 :(得分: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());
        }
    }



}