[英]Get values of types from XML File in C# in a list
I Want to get values from XML in C# in list. 我想从列表中的C#中的XML获取值。 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".
有一些特定条件,例如,我需要在要获取名称的属性中显示Ruleid,dataprovider,在需要获取类型为“ Healthy”的value(20),operator(大于或小于)的条件下。
Example XML. XML示例。
"<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>
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();
}
}
}
How to achieve in simple and better way? 如何以简单更好的方式实现?
When dealing with xml I usually avoid to parse manually (unless the xml is not known apriori). 在处理xml时,我通常避免手动解析(除非xml不为先验)。 You can generate a parser using XSD.exe.
您可以使用XSD.exe生成解析器。
Deserialize your xml file: 反序列化您的xml文件:
XmlSerializer serializer = new XmlSerializer(typeof(YourType)); XmlSerializer序列化器=新的XmlSerializer(typeof(YourType));
StreamReader reader = new StreamReader(yourXmlPath); StreamReader reader =新的StreamReader(yourXmlPath);
var yourStronglyTypedObject = (YourType)serializer.Deserialize(reader); var yourStronglyTypedObject =(YourType)serializer.Deserialize(reader); reader.Close();
reader.Close();
Play with your strongly typed object 玩您的强类型对象
Is worth to spend 1 hour and learn xsd: You probably will have to change the xsd a little to better reflect you xml format. 值得花费1个小时来学习xsd:您可能需要对xsd进行一些更改才能更好地反映您的xml格式。 Autogenerated xsd is usually more general and permissive eg.
自动生成的xsd通常更为通用和宽松,例如。 tend to use collections more than needed (just fix maxOccurs / minOccurs attribute)
倾向于使用比需要更多的集合(只需修复maxOccurs / minOccurs属性)
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.