2017-08-18 2 views
1

私は5つの項目を含むリストmyListOfItemsを持っています。親タグなしでオブジェクトのリストをシリアライズ

<something> 
    <someValue/> 
</something> 

<item>...</item> 
<item>...</item> 
<item>...</item> 
<item>...</item> 
<item>...</item> 

<somethingElse> 
    <someValue/> 
</somethingElse> 

とこれまでのところ、私はこれがあります:

<something> 
    <someValue/> 
</something> 

<myListOfItems> 
    <item>...</item> 
    <item>...</item> 
    <item>...</item> 
    <item>...</item> 
    <item>...</item> 
</myListOfItems> 

<somethingElse> 
    <someValue /> 
</somethingElse> 

そして、これは私がそれを行う方法です。

を私がやりたいすべては、すべての項目がそのように表示されるXMLを作成することです
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=34)] 
[System.Xml.Serialization.XmlArrayItemAttribute("passengerDetails", IsNullable=false)] 
public List<Item> myListOfItems { 
    get { 
     return this.myListOfItemsField; 
    } 
    set { 
     this.myListOfItemsField = value; 
     this.RaisePropertyChanged("myListOfItems"); 
    } 
} 

親myListOfItemsタグを省略するにはどうすればよいですか?

編集:あなたが言及したトピックについて : 私はのXmlElementにXmlArrayItemAttributeを変更すると、私はこの

InvalidOperationExceptionが取得:のXmlElementは、のXmlText、およびXmlAnyElementがXmlAttribute、XmlAnyAttribute、XmlArrayと一緒に使用することはできません、またはXmlArrayItem。

そして、私はXmlArrayAttribute部分をコメントアウトするとき、私はこの

InvalidOperationExceptionが取得:一貫性のないシーケンシング:クラスのメンバーの一人に使われている場合は、「注文」プロパティは、すべての粒子状部材に必要とされるがクラスメンバー 'item'のXmlElement、XmlAnyElement、XmlArrayカスタム属性を使用して明示的に 'Order'を設定してください。

複数の商品の注文を設定するにはどうすればよいですか?

答えて

0

serializeを使用する必要はありません。代わりにxmlを使用するlinq:

using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 
using System.IO; 
using System.Net; 


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

      XDocument doc = XDocument.Load(FILENAME); 

      List<string> items = doc.Descendants("item").Select(x => (string)x).ToList(); 

     } 

    } 

} 
関連する問題