-2

Actually I Want to get values from XML in C# in list. There are some specific conditions like. I need to show ruleid, dataprovider, in attribute I want to get name, in conditions in need to get value(20),operator(greaterthan or lessthan) of type="Healthy".

I have attached a image for the example XML. enter image description here

I tried to parse the data in the following way :

public static void readXml()
    {
        XmlDocument xmldoc = new XmlDocument();
        XmlNodeList xmlnode;
        int i = 0;
        List<Rule> listx = new List<Rule>();

        FileStream fs = new FileStream("C://ConsoleApplication1//sample_manifest.xml", FileMode.Open, FileAccess.Read);
        xmldoc.Load(fs);
        xmlnode = xmldoc.GetElementsByTagName("attribute", "condition");
         XmlNodeList list = xmldoc.SelectNodes(@"/psmsmanifiest/rules/rule/attributes");

         foreach (XmlNode node in list)
        {
            foreach (XmlNode childNode in node.ChildNodes)
            {

                //string dataprovider = node["Dataprovider"].Attributes.Item(0);
                var attribute = node["attribute"].InnerXml;
                Console.WriteLine(attribute);
                Console.ReadLine();

         }
        }
    }
Sonam G
  • 118
  • 1
  • 15
  • 1
    There seems to be something wrong with your question formatting – Sossenbinder Sep 04 '15 at 13:52
  • Edit: Ok I see the picture now. What did you try so far to evaluate your XML? – Sossenbinder Sep 04 '15 at 13:53
  • This question may be a duplicate. Please see this answer: http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – Chris Bohatka Sep 04 '15 at 14:05
  • possible duplicate of [How does one parse XML files?](http://stackoverflow.com/questions/55828/how-does-one-parse-xml-files) – Ryan Emerle Sep 04 '15 at 14:15
  • 4
    Please include a sample of the code that you've tried so far. Also an _image_ of _text_ isn't very useful for folks who'd like to write sample code against your data. For some guidance, check out how to create a [mcve]. – Ryan Emerle Sep 04 '15 at 14:19

1 Answers1

0

Try code below. I fixed the xml tag attribute.

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();
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I tried your code and getting an error "Data at the root level is invalid" at line XDocument doc = XDocument.Parse(input); – Sonam G Sep 08 '15 at 07:12
  • Code posted runs without errors. Did you make any modifications? What version on Net Library are you using? – jdweng Sep 08 '15 at 08:08
  • yes, i tried using the path of my xml document inplace of input : "C://ConsoleApplication1//sample_manifest.xml" – Sonam G Sep 08 '15 at 08:09
  • I added the code which I tried. I know its HArdcoded, and I dont want to do in my way. – Sonam G Sep 08 '15 at 08:10
  • What don't you want hardcoded? Some of the tags have to be predefined. The tag names are strings and can be replaced with string variables. – jdweng Sep 08 '15 at 09:12