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.
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();
}
}
}
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.