如何使用XSD生成的类来填充和调整XML

时间:2010-10-14 11:50:59

标签: c# xml xsd

使用xds.exe我创建了以下类:

namespace vgtunesWPF
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Library
{

    private LibraryTrack[] mediaField;

    private string playListsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Track", IsNullable = false)]
    public LibraryTrack[] Media
    {
        get
        {
            return this.mediaField;
        }
        set
        {
            this.mediaField = value;
        }
    }

    /// <remarks/>
    public string PlayLists
    {
        get
        {
            return this.playListsField;
        }
        set
        {
            this.playListsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class LibraryTrack
{

    private string idField;

    private string locationField;

    private string titleField;

    private string artistField;

    private string albumField;

    private string jaarField;

    private string genreField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "ID")]
    public string ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    public string Location
    {
        get
        {
            return this.locationField;
        }
        set
        {
            this.locationField = value;
        }
    }

    /// <remarks/>
    public string Title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }

    /// <remarks/>
    public string Artist
    {
        get
        {
            return this.artistField;
        }
        set
        {
            this.artistField = value;
        }
    }

    /// <remarks/>
    public string Album
    {
        get
        {
            return this.albumField;
        }
        set
        {
            this.albumField = value;
        }
    }

    /// <remarks/>
    public string Jaar
    {
        get
        {
            return this.jaarField;
        }
        set
        {
            this.jaarField = value;
        }
    }

    /// <remarks/>
    public string Genre
    {
        get
        {
            return this.genreField;
        }
        set
        {
            this.genreField = value;
        }
    }
}
}

它用于创建以下XML:

<Library>
 <Media>
    <Track>
      <ID>0</ID>
      <Location>url</Location>
      <Title>title</Title>
      <Artist>artist</Artist>
      <Album>album</Album>
      <Jaar>year</Jaar>
      <Genre>genre</Genre>
    </Track>
      <ID>1</ID>
      <Location>url</Location>
      <Title>title</Title>
      <Artist>artist</Artist>
      <Album>album</Album>
      <Jaar>year</Jaar>
      <Genre>genre</Genre>
    </Track>
 </Media>
</Library>

这里的问题是,我不知道如何在保持数据存储的同时填充xml。

1 个答案:

答案 0 :(得分:1)

听起来您想要将现有的XML数据与存储在类中的新数据合并。

最好的方法是将xml反序列化为该类的实例,进行更改然后再次序列化。