2009-06-18 11 views
37

シリアライズとオブジェクトに問題がありますが、値と属性が必要な要素を除いて正しい出力をすべて作成することができます。ここで必要な出力です:C#XMLシリアライゼーションの属性を追加する方法

<Root> 
    <Method>Retrieve</Method> 
    <Options> 
    <Filter> 
     <Times> 
     <TimeFrom>2009-06-17</TimeFrom> 
     </Times> 
     <Document type="word">document name</Document> 
    </Filter> 
    </Options> 
</AdCourierAPI> 

私はそれのすべてを構築することができますが、ドキュメントタイプの属性を設定する方法を見つけることができませんが、ここで私を与えるオブジェクトクラス

[XmlRoot("Root"), Serializable]  
public class Root  
{  
    [XmlElement("Method")]  
    public string method="RetrieveApplications";  
    [XmlElement("Options")]  
    public _Options Options;  
}  
public class _Options  
{ 
    [XmlElement("Filter")]  
    public _Filter Filter;  
} 
public class _Filter  
{ 
    [XmlElement("Times")]  
    public _Times Times;  
    [XmlElement("Documents")]  
    public string Documents;  
} 

のセグメントであります:

<Document>document name</Document> 

いうより:

<Document type="word">document name</Document> 

しかし、私はこれを修正する方法を見つけることができません、助言してください。

おかげ

+0

申し訳ありませんがマルクを...あなたはちょうど私の前にその編集を得たに違いありません。 – Kev

+0

Mark Gravellが私の一日を救った! :-) ありがとうございました! –

答えて

56

ませんか?

通常は、のようなものかもしれない:

class Document { 
    [XmlAttribute("type")] 
    public string Type { get; set; } 
    [XmlText] 
    public string Name { get; set; } 
} 


public class _Filter  
{ 
    [XmlElement("Times")]  
    public _Times Times;  
    [XmlElement("Document")]  
    public Document Document;  
} 
+0

ありがとう、ソートあり、ありがとうございました – user107779

+0

私のために働いていません –

+0

これはすごく素晴らしいです。 – Sizons

11

stringクラスはtype性質を持っていないので、あなたは、所望の出力を作成するために使用することはできません。

public class Document 
{ 
    [XmlText] 
    public string Name; 

    [XmlAttribute("type")] 
    public string Type; 
} 

を、あなたが余分なクラスが必要のようですねDocument

+0

私は同じことを提出しようとしていました。 –

7

を入力するDocumentプロパティを変更する必要があります:あなたは代わりにDocumentクラスを作成する必要があり

​​

インスタンス(中例)はType = "word"Name = "document name"です。 documentsList<Document>となります。ところで

- 公共の場ではめったに良いアイデア...あなたはtypeが保存されていない

関連する問題