Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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#_.net_Xml_Xml Parsing - Fatal编程技术网

C# 字段类型的XML到对象转换

C# 字段类型的XML到对象转换,c#,.net,xml,xml-parsing,C#,.net,Xml,Xml Parsing,我需要根据我的xml创建对象,这个对象将被用作c中Web服务的输入 如果我有这个xml <v1:Field type="Note"> <v1:name>Buyer’s Name should be as per Passport / Trade License. For existing Emaar property owners, please provide details as per existing profile.</v1:name>

我需要根据我的xml创建对象,这个对象将被用作c中Web服务的输入

如果我有这个xml

<v1:Field type="Note">
   <v1:name>Buyer’s Name should be as per Passport / Trade License. For existing Emaar property owners, please provide details as per existing profile.</v1:name>
   <v1:category>MESSAGE</v1:category>
   <v1:mandatory>N</v1:mandatory>
   <v1:alias>DESCLAIMER_FYI</v1:alias>
   <v1:value>Buyer’s Name should be as per Passport / Trade License. For existing Emaar property owners, please provide details as per existing profile.</v1:value>
</v1:Field>
哪个属性将用于获取此值 ?


若我将类型设置为公共属性,那个么它将以xml中的标记形式出现,比如名称、类别,但我想使用字段标记,它被称为属性。我可以在C中使用什么作为字段标记附带的属性。

试试xml linq。我的测试代码如下:

public class SomeIntInfo
{
    [XmlAttribute]
    public int Value { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement root = doc.Root;
            XNamespace ns = root.GetNamespaceOfPrefix("v1");

            var results = root.Descendants(ns + "Field").Select(x => new Field() {
                type = (string)x.Attribute("type"),
                name = (string)x.Element(ns + "name"),
                category = (string)x.Element(ns + "category"),
                mandatory = (string)x.Element(ns + "mandatory"),
                alias = (string)x.Element(ns + "alias"),
                value = (string)x.Element(ns + "value"),
            }).FirstOrDefault();
        }
    }
    public class Field
    {
        public string type { get; set; }
        public string name { set; get; }
        public string category { set; get; }
        public string mandatory { set; get; }
        public string alias { set; get; }
        public string value { set; get; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement root = doc.Root;
            XNamespace ns = root.GetNamespaceOfPrefix("v1");

            var results = root.Descendants(ns + "Field").Select(x => new Field() {
                type = (string)x.Attribute("type"),
                name = (string)x.Element(ns + "name"),
                category = (string)x.Element(ns + "category"),
                mandatory = (string)x.Element(ns + "mandatory"),
                alias = (string)x.Element(ns + "alias"),
                value = (string)x.Element(ns + "value"),
            }).FirstOrDefault();
        }
    }
    public class Field
    {
        public string type { get; set; }
        public string name { set; get; }
        public string category { set; get; }
        public string mandatory { set; get; }
        public string alias { set; get; }
        public string value { set; get; }
    }
}