簡體   English   中英

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

[英]Extracting values from serialized XML in C#

我有以下序列化的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標記的值,但沒有其他值。 如何解決這個問題。

提前致謝。

嘗試這個

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);
}

XDocumentXmlDocument更新。 更具可讀性,您可以使用LINQ。

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

您可以使用Linq到XML:

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

結果: 在此處輸入圖片說明

試試這個代碼:

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);
        }
    }
}

試試這個代碼:

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);
    }
}

我喜歡在使用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());
        }
    }



}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM