Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/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如何循环使用XMLNode获取的XML节点。选择SingleNode_C#_Xml - Fatal编程技术网

c#,XML如何循环使用XMLNode获取的XML节点。选择SingleNode

c#,XML如何循环使用XMLNode获取的XML节点。选择SingleNode,c#,xml,C#,Xml,我有一个类,它在XML中表示一个节点测试用例: public class TestCase { [XmlAttribute("name")] public string name { get; set; } public string version { get; set; } public string verdict { get; set; } public string objective { get; set; } public strin

我有一个类,它在XML中表示一个节点测试用例:

public class TestCase
{
    [XmlAttribute("name")]
    public string name { get; set; }
    public string version { get; set; }
    public string verdict { get; set; }
    public string objective { get; set; }
    public string criteria { get; set; }
    public string issue { get; set; }
    public string clientcomments { get; set; }
    public string authoritycomments { get; set; }
    public string sdk { get; set; }   

}
我使用XmlNode.SelectSingleNode获取XML中的特定节点。对于info,如果重要的话,没有重复的节点(没有具有相同名称属性的节点)

到目前为止,我有以下代码:

public static TestCase FetchNode(string NodeName, string Path)
    {
        TestCase testcase = new TestCase();
        string[] attr = { "name", "version", "verdict", "objective", "criteria"
                , "issue", "clientcomments", "authoritycomments", "sdk" };
        string[] attrval = { null, null,null,null,null,null,null,null,null};

        XmlDocument doc = new XmlDocument();
        doc.Load(Path);

        XmlNode node = doc.SelectSingleNode("/TestsList/TestCase[@name='" + NodeName + "']");

        for (var i = 0; i == attr.Length - 1;i++)
        {
            attrval[i] = node[attr[i]].InnerText;
        }

        testcase.name = attrval[0];
        testcase.version = attrval[1];
        testcase.verdict = attrval[2];
        testcase.objective = attrval[3];
        testcase.criteria = attrval[4];
        testcase.issue = attrval[5];
        testcase.clientcomments = attrval[6];
        testcase.authoritycomments = attrval[7];
        testcase.sdk = attrval[8];

        return testcase;

    }
然而,这段代码是不可伸缩的,如果我改变我的类结构,我将需要改变函数,因为类的每个元素都是硬编码的

这是一个广泛的请求,但是我如何编写这个函数呢?因此,如果我在TestCase的类定义中添加或删除一个字符串,我就不必更改FetchNode函数

谢谢你抽出时间

您可以使用

例如:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class TestCase
{
    [XmlAttribute("name")]
    public string name { get; set; }
    public string version { get; set; }
    public string verdict { get; set; }
    public string objective { get; set; }
    public string criteria { get; set; }
    public string issue { get; set; }
    public string clientcomments { get; set; }
    public string authoritycomments { get; set; }
    public string sdk { get; set; }   
}

public class Program
{
    public const string XML = @"
<TestCase name='TicketName'>
    <name>Jon Nameson</name>
    <version>10.1</version>
    <verdict>High</verdict>
</TestCase>
";

    public static void Main()
    {
        var doc = new XmlDocument();
        doc.LoadXml(XML);

        var node = doc.SelectSingleNode("/TestCase");

        var serializer = new XmlSerializer(typeof(TestCase));

        var testcase = serializer.Deserialize(new StringReader(node.OuterXml)) as TestCase;

        Console.WriteLine(testcase.name);
        Console.WriteLine(testcase.version);
        Console.WriteLine(testcase.verdict);
    }
}
使用系统;
使用System.IO;
使用System.Xml;
使用System.Xml.Serialization;
公共类测试用例
{
[XmlAttribute(“名称”)]
公共字符串名称{get;set;}
公共字符串版本{get;set;}
公共字符串判决{get;set;}
公共字符串目标{get;set;}
公共字符串条件{get;set;}
公共字符串问题{get;set;}
公共字符串clientcomments{get;set;}
公共字符串authoritycomments{get;set;}
公共字符串sdk{get;set;}
}
公共课程
{
公共常量字符串XML=@“
乔恩·纳森
10.1
高
";
公共静态void Main()
{
var doc=新的XmlDocument();
doc.LoadXml(XML);
var node=doc.SelectSingleNode(“/TestCase”);
var serializer=newxmlserializer(typeof(TestCase));
var testcase=serializer.Deserialize(新的StringReader(node.OuterXml))作为testcase;
Console.WriteLine(testcase.name);
WriteLine(testcase.version);
Console.WriteLine(testcase.Decision);
}
}
您可以使用

例如:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class TestCase
{
    [XmlAttribute("name")]
    public string name { get; set; }
    public string version { get; set; }
    public string verdict { get; set; }
    public string objective { get; set; }
    public string criteria { get; set; }
    public string issue { get; set; }
    public string clientcomments { get; set; }
    public string authoritycomments { get; set; }
    public string sdk { get; set; }   
}

public class Program
{
    public const string XML = @"
<TestCase name='TicketName'>
    <name>Jon Nameson</name>
    <version>10.1</version>
    <verdict>High</verdict>
</TestCase>
";

    public static void Main()
    {
        var doc = new XmlDocument();
        doc.LoadXml(XML);

        var node = doc.SelectSingleNode("/TestCase");

        var serializer = new XmlSerializer(typeof(TestCase));

        var testcase = serializer.Deserialize(new StringReader(node.OuterXml)) as TestCase;

        Console.WriteLine(testcase.name);
        Console.WriteLine(testcase.version);
        Console.WriteLine(testcase.verdict);
    }
}
使用系统;
使用System.IO;
使用System.Xml;
使用System.Xml.Serialization;
公共类测试用例
{
[XmlAttribute(“名称”)]
公共字符串名称{get;set;}
公共字符串版本{get;set;}
公共字符串判决{get;set;}
公共字符串目标{get;set;}
公共字符串条件{get;set;}
公共字符串问题{get;set;}
公共字符串clientcomments{get;set;}
公共字符串authoritycomments{get;set;}
公共字符串sdk{get;set;}
}
公共课程
{
公共常量字符串XML=@“
乔恩·纳森
10.1
高
";
公共静态void Main()
{
var doc=新的XmlDocument();
doc.LoadXml(XML);
var node=doc.SelectSingleNode(“/TestCase”);
var serializer=newxmlserializer(typeof(TestCase));
var testcase=serializer.Deserialize(新的StringReader(node.OuterXml))作为testcase;
Console.WriteLine(testcase.name);
WriteLine(testcase.version);
Console.WriteLine(testcase.Decision);
}
}

您可以直接从所选的
XmlNode
反序列化,方法是结合使用以下扩展方法:

public static class XmlNodeExtensions
{
    public static T Deserialize<T>(this XmlNode element, XmlSerializer serializer = null)
    {
        using (var reader = new ProperXmlNodeReader(element))
            return (T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
    }

    class ProperXmlNodeReader : XmlNodeReader
    {
        // Bug fix from https://stackoverflow.com/questions/30102275/deserialize-object-property-with-stringreader-vs-xmlnodereader
        public ProperXmlNodeReader(XmlNode node)
            : base(node)
        {
        }

        public override string LookupNamespace(string prefix)
        {
            return NameTable.Add(base.LookupNamespace(prefix));
        }
    }
}
这与:

var testcase = XmlNodeExtensions.Deserialize<TestCase>(node);
var testcase=XmlNodeExtensions.Deserialize(节点);

在您的情况下,
TestCase
类的预期根元素名称,即
,与实际节点名称匹配。如果节点名称与预期的根元素名称不匹配,您可以按照中的说明告诉
XmlSerializer
预期不同的根名称。

您可以通过与使用以下扩展方法组合直接从所选
XmlNode
反序列化:

public static class XmlNodeExtensions
{
    public static T Deserialize<T>(this XmlNode element, XmlSerializer serializer = null)
    {
        using (var reader = new ProperXmlNodeReader(element))
            return (T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
    }

    class ProperXmlNodeReader : XmlNodeReader
    {
        // Bug fix from https://stackoverflow.com/questions/30102275/deserialize-object-property-with-stringreader-vs-xmlnodereader
        public ProperXmlNodeReader(XmlNode node)
            : base(node)
        {
        }

        public override string LookupNamespace(string prefix)
        {
            return NameTable.Add(base.LookupNamespace(prefix));
        }
    }
}
这与:

var testcase = XmlNodeExtensions.Deserialize<TestCase>(node);
var testcase=XmlNodeExtensions.Deserialize(节点);

在您的情况下,
TestCase
类的预期根元素名称,即
,与实际节点名称匹配。如果节点名称与预期的根元素名称不匹配,您可以按照中的说明告诉
XmlSerializer
预期不同的根名称。

感谢您的详细响应。我的c#理解能力没有那么先进,所以我不确定您提供给我的代码到底是什么。你能给我简单解释一下代码吗?谢谢@杰普加农-我补充了一个简短的解释,但我不确定你需要知道什么。这更多的是对你所做的事情的一般理解。你提供的链接给了我更多的信息。我将Negorath response标记为答案,因为我可以理解所做的事情,但我肯定也会尝试这种方法(更重要的是,理解它)。谢谢。谢谢你的详细回复。我的c#理解能力没有那么先进,所以我不确定您提供给我的代码到底是什么。你能给我简单解释一下代码吗?谢谢@杰普加农-我补充了一个简短的解释,但我不确定你需要知道什么。这更多的是对你所做的事情的一般理解。你提供的链接给了我更多的信息。我将Negorath response标记为答案,因为我可以理解所做的事情,但我肯定也会尝试这种方法(更重要的是,理解它)。谢谢。工作顺利,没有太多变化。谢谢。工作顺利,没有太多变化。非常感谢。