2017-10-19 4 views
0

私のXMLごとにオブジェクトを作成する必要があります。このオブジェクトは、C#のWebサービスの入力として使用されます。フィールド型のXMLからオブジェクトへの変換

私はこの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> 

を持っている場合、私は、それに基づいて属性がこの値 < V1を取るために使用される例えば

public class Field 
{ 
    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; } 
} 

をクラスを作ることができます。フィールドタイプ= "注">

私はパブリックプロパティとして型を作ると、それはxmlのような名前のタグとして来るでしょう、カテゴリが来ますが、私は属性と呼ばれるFieldタグでそれを作りたいと思います。 Fieldタグを持つ属性として機能するC#で何が使えますか?

答えて

1
public class SomeIntInfo 
{ 
    [XmlAttribute] 
    public int Value { get; set; } 
} 
0

try xml linq。私は以下のコードをテストします:

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; } 
    } 
}