Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从列表中C#中的XML文件中获取类型值_C#_Xml - Fatal编程技术网

从列表中C#中的XML文件中获取类型值

从列表中C#中的XML文件中获取类型值,c#,xml,C#,Xml,我想从C#in列表中的XML中获取值。有一些特定的条件,比如,我需要在我想要获取名称的属性中显示ruleid,dataprovider, 在需要获取值(20)的情况下,类型为“健康”的运算符(大于或小于) 示例XML "<psmsmanifiest version=\"2\" lastmodified=\"2015-08-06 03:53:06.207\">" + "<rules>" + "<!--samp

我想从C#in列表中的XML中获取值。有一些特定的条件,比如,我需要在我想要获取名称的属性中显示ruleid,dataprovider, 在需要获取值(20)的情况下,类型为“健康”的运算符(大于或小于)

示例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>
“”+
"" +
"" +
"" +
"" +
"" +
"" +
"100>" +
"100>" +
"" +
"" +
"" +
"" +
"100>" +
"100>" +
"" +
"" +
"" +
"" +
"" +
"
我尝试以以下方式解析数据:

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();

         }
        }
    }
publicstaticvoidreadxml()
{
XmlDocument xmldoc=新的XmlDocument();
XmlNodeList xmlnode;
int i=0;
List listx=新列表();
FileStream fs=newfilestream(“C://ConsoleApplication1//sample_manifest.xml”,FileMode.Open,FileAccess.Read);
xmldoc.Load(fs);
xmlnode=xmldoc.GetElementsByTagName(“属性”、“条件”);
XmlNodeList=xmldoc.SelectNodes(@“/psmsmanifiest/rules/rule/attributes”);
foreach(列表中的XmlNode节点)
{
foreach(node.ChildNodes中的XmlNode-childNode)
{
//字符串dataprovider=节点[“dataprovider”]。属性。项(0);
var attribute=node[“attribute”]。InnerXml;
Console.WriteLine(属性);
Console.ReadLine();
}
}
}

如何以更简单更好的方式实现?

在处理xml时,我通常避免手动解析(除非xml事先未知)。您可以使用XSD.exe生成解析器

  • 打开visual studio命令行
  • 或者(为了方便起见),将目录更改为xml文件位置,以便在同一位置生成文件,而不是在xsd.exe所在的位置生成文件
  • 编写“xsd.exe”以从xml生成xsd文件
  • 编写“xsd/c”以生成能够解析xml的c#类
  • 在项目中导入.cs文件
  • 反序列化xml文件:

    XmlSerializer serializer=新的XmlSerializer(typeof(YourType));
    StreamReader=新的StreamReader(yourXmlPath);
    var yourStronglyTypedObject=(YourType)序列化程序。反序列化(读取器); reader.Close()

  • 使用强类型对象

  • 值得花1小时学习xsd:您可能需要稍微更改xsd以更好地反映您的xml格式。自动生成的xsd通常更为通用和允许,例如,倾向于使用超出需要的集合(仅修复maxOccurs/minOccurs属性)


希望这能有所帮助。

您能将XML作为文本而不是图像添加吗?@MattJones,我已经以文本格式添加了XML。正常情况下,仅迭代元素会有什么问题?XML的结构会有所不同吗?使用搜索看起来很脏,而且容易出错。