简体   繁体   中英

Serialize to xml using a class with multiple classes

I am using an asp.net and C# to serialize some entered data using a class. This class was generated from an xsd file. I am using this class to generate an xml file. I am struggling to understand how I can reference all these classes and serialize it to one xml file.

Here is my class code. It is in a separate file within my project:

 namespace Ce
{
    using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
// 



public partial class AddRequest
{

    private CDocument cDocumentField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "x")]
    public CDocument CDocument
    {
        get
        {
            return this.cDocumentField;
        }
        set
        {
            this.cDocumentField = value;
        }
    }
}


public partial class CDocument
{

    private CaseA caseAField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "x")]
    public CaseA CaseA
    {
        get
        {
            return this.caseAField;
        }
        set
        {
            this.caseAField = value;
        }
    }
}


public partial class CaseA
{

    public string dCTextField;

    private string dLField;



    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "x")]
    public string DCText
    {
        get
        {
            return this.dCTextField;
        }
        set
        {
            this.dCTextField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "x")]
    public string DLU
    {
        get
        {
            return this.dLField;
        }
        set
        {
            this.dLField = value;
        }
    }

}
}

To serialize I have this in my code-behind in a button click event:

 protected void CaseSubmitButton_Click(object sender, EventArgs e)
    {

CaseA CSerialize = new CaseA();
    CSerialize.DCText = casetextboxt.text;
    CSerialize.DLu = "\\app";

    XmlSerializer Serializer = new XmlSerializer(typeof(CaseA));
    StreamWriter Writer = new StreamWriter(Server.MapPath("~/XmlPackages/" + xmlPackageFilename));
    Serializer.Serialize(Writer, CSerialize);
    Serializer.Serialize(Writer, CSerialize); 

}

With this button click event I get this xml format:

<?xml version="1.0" encoding="utf-8"?>
<CaseA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <DCText>\apptest</DCText>
    <DLU>\apptest</DLU>

</CaseA>

I would like to reference the other two classes to get:

    <?xml version="1.0" encoding="utf-8"?>
<Addrequest>
 <Cdocument>
   <CaseA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <DCText>\apptest</DCText>
    <DLU>\apptest</DLU>

   </CaseA>
  </Cdocument>
</Addrequest>

You're serializing an instance of CaseA class, which is why you're only seeing members of CaseA in the output. You need to create an instance of AddRequest and serialize that as the root, something like this:

CaseA cSerialize = new CaseA();
cSerialize .DCText = casetextboxt.text;
cSerialize .DLu = "\\app";

CDocument document = new CDocument();
document.CaseA = cSerialize ;

AddRequest root = new AddRequest();
root.CDocument = document;

XmlSerializer serializer = new XmlSerializer(typeof(AddRequest), new Type[] { typeof(CDocument), typeof(CaseA) });
StreamWriter writer = new StreamWriter(Server.MapPath("~/XmlPackages/" + xmlPackageFilename));
serializer.Serialize(writer, root);

As a side note, please follow C# coding standards for naming your local variables :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM