2017-11-13 7 views
1

これは私のXMLファイルです。私は上記のXML以下内部タグと内部要素を使用してXML文書を逆シリアル化する方法

をデシリアライズしたい

<getMetadata> 
<Project Name="Doors_Demo"> 
    <Module FullPath="/Doors_Demo/Test_Module2"> 
    <Attributes> 
    <Attribute name="TableType" type="TableType" /> 
    <Attribute name="TableTopBorder" type="TableEdgeType" /> 
    </Attributes> 
    </Module> 
</Project> 
</getMetadata> 

が私のコードです:

[XmlRoot("getMetadata")] 
public class RootClass 
{ 
    public Project element_Project; 

    [XmlElement("Project")] 
    public Project Project 
    { 
     get { return element_Project; } 
     set { element_Project = value; } 
    } 
} 

public class Project 
{ 
    public string name; 

    [XmlAttribute("Name")] 
    public string Id 
    { 
     get { return name; } 
     set { name = value; } 
    } 
} 

public static void Main(string[] args) 
{ 
    RootClass obj = new RootClass(); 

    XmlSerializer serializer = new XmlSerializer(typeof(RootClass)); 
    using (FileStream stream = new FileStream(@"E:\getMetadata(4).xml", FileMode.Open)) 
    { 
     RootClass myxml = (RootClass)serializer.Deserialize(stream); 

     Console.WriteLine(myxml.Project.name); 
    } 
} 

私はリストに私のXMLをデシリアライズしたい、私はすべての内部要素にアクセスすることはできないですし、ルート要素内の属性。

私は、モジュール要素とその内部要素とタグの詳細を、アクセス可能なリストに入れたいと思います。

+1

「属性」という要素を持つ文書のpuke-emoji ... –

答えて

2

XMLからクラスを自動的に生成するための小さなトリックです。

最初に、新しい空のクラスを作成します(例:TempXml)。

XMLをクリップボードにコピーし、作成した新しい空のクラスを開きます。 Visual Studioの編集メニューへ

ゴーその後、クラスとして特別と貼り付けXMLを貼り付けます。べき

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class getMetadata 
{ 

    private getMetadataProject projectField; 

    /// <remarks/> 
    public getMetadataProject Project 
    { 
     get 
     { 
      return this.projectField; 
     } 
     set 
     { 
      this.projectField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class getMetadataProject 
{ 

    private getMetadataProjectModule moduleField; 

    private string nameField; 

    /// <remarks/> 
    public getMetadataProjectModule Module 
    { 
     get 
     { 
      return this.moduleField; 
     } 
     set 
     { 
      this.moduleField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class getMetadataProjectModule 
{ 

    private getMetadataProjectModuleAttribute[] attributesField; 

    private string fullPathField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlArrayItemAttribute("Attribute", IsNullable = false)] 
    public getMetadataProjectModuleAttribute[] Attributes 
    { 
     get 
     { 
      return this.attributesField; 
     } 
     set 
     { 
      this.attributesField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string FullPath 
    { 
     get 
     { 
      return this.fullPathField; 
     } 
     set 
     { 
      this.fullPathField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class getMetadataProjectModuleAttribute 
{ 

    private string nameField; 

    private string typeField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string type 
    { 
     get 
     { 
      return this.typeField; 
     } 
     set 
     { 
      this.typeField = value; 
     } 
    } 
} 

Paste XML as Classes

これは、次のコードを生成しますXmlSerializerクラスでうまく動作します。

空の注釈を削除し、キャメルケースを使用するようにクラスの名前を変更することで、生成された出力を少しきれいにすることができます(この場合、属性の実際の要素名を指定する必要がありますあなたの質問)またはクラスを別のファイルに移動します。

希望します。

+0

私はプロジェクト名を取得できますが、モジュール名とXMLから属性を取得したいのですが、どうすればいいですか? – Abby

関連する問題