简体   繁体   中英

Get values of types from XML File in C# in a list

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".

Example 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). You can generate a parser using XSD.exe.

  • Open a visual studio command line.
  • Optionally (for convenience), change directory to you xml file location so that files will be generate in the same place and not where xsd.exe is located.
  • Write 'xsd.exe ' to generate a xsd file from your xml.
  • Write 'xsd /c to generate a C# class capable of parsing you xml.
  • Import .cs file in your project
  • Deserialize your xml file:

    XmlSerializer serializer = new XmlSerializer(typeof(YourType));
    StreamReader reader = new StreamReader(yourXmlPath);
    var yourStronglyTypedObject = (YourType)serializer.Deserialize(reader); 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. Autogenerated xsd is usually more general and permissive eg. tend to use collections more than needed (just fix maxOccurs / minOccurs attribute)

Hope this helps.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM