想要从列表中的C#中获取XML值

时间:2015-09-04 13:52:15

标签: c# xml

实际上我想从列表中的C#中获取XML值。有一些特定的条件,如。 我需要表明 ruleid, 数据提供器, 在属性我想得到名字, 在需要获得值(20),运营商(greaterthan或lessthan)类型=“健康”的条件下。

我为示例XML附加了一个图像。 enter image description here

我尝试按以下方式解析数据:

{{1}}

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。我修复了xml标记属性。

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<psmsmanifiest version=\"2\" lastmodified=\"2015-08-06 03:53:06.207\">" +
                  "<rules>" +
                    "<!--sample for runtime data provider-->" +
                    "<rule ruleid=\"8504dcad-f748-4add-9e95-239d5382f1c6\" dataprovider=\"runtime\">" +
                      "<attributes>" +
                        "<attribute name=\"platform.attibute1.value\" type=\"int\">" +
                          "<conditions>" +
                            "<condition type=\"healthy\" operator=\"greaterthan\">100></condition>" +
                            "<condition type=\"unhealthy\" operator=\"greaterthanequal\">100></condition>" +
                          "</conditions>" +
                        "</attribute>" +
                        "<attribute name=\"platform.attibute2.value\" type=\"int\">" +
                          "<conditions>" +
                            "<condition type=\"healthy\" operator=\"greaterthan\">100></condition>" +
                            "<condition type=\"unhealthy\" operator=\"greaterthanequal\">100></condition>" +
                          "</conditions>" +
                        "</attribute>" +
                      "</attributes>" +
                    "</rule>" +
                  "</rules>" +
                "</psmsmanifiest>";

            XDocument doc = XDocument.Parse(input);
            var results = doc.Descendants("rule").Select(x => new
            {
                ruleid = x.Attribute("ruleid").Value,
                dataprovider = x.Attribute("dataprovider").Value,
                attributes = x.Descendants("attribute").Select(y => new
                {
                    name = y.Attribute("name").Value,
                    conditions = y.Descendants("condition").Select(z => new
                    {
                        _type = z.Attribute("type").Value,
                        _operator = z.Attribute("operator").Value,
                        _value = z.Value
                    }).ToList()
                }).ToList()
            }).ToList();
        }
    }
}
​