简体   繁体   中英

C# .Net - write data in xml by using existing structure/schema

I have an API which creates a sample xml based on a schema file. Here is the link to the API: http://msdn.microsoft.com/en-us/library/aa302296.aspx

Now, suppose the sample xml generated is as follows:

<Employee>
   <Name>Bond</Name>
   <Address>abc,unknown street</Address>
   <phone>0000000000</phone>
<Employee>

I have a employee DB table consisting of thousands of employee records. What I want is to write those records in the xml format created above (table has many non-required columns also so can't use dataset.writexml ).

What is the right approach of doing this ? Should I edit the first node as per the first record and then copy the whole node into same xml, write the record then repeat the process till end of records? Or is there a better approach of doing this?

Here is an example of serializing and Deserializing Objects to xml.

using System;
using System.Xml.Serialization;
using System.IO;

namespace Test
{
[Serializable]
[XmlRoot("DocumentElement")]
public class Documentelement
{
    [XmlElement]
    public PartInfo[] PartInfo { get; set; }
}

public class PartInfo
{
    [XmlElement]
    public int ID { get; set; }
    public string Name { get; set; }
    public string PartNo { get; set; }
    public int SerialNo { get; set; }
    public string Parameter { get; set; }        
    public DateTime InstallDate { get; set; }
    public DateTime InstallTill { get; set; }
}

public class Test
{

    private PartInfo details_1()
    {
        PartInfo details = new PartInfo
        {
            ID = 0,
            Name = "QVR",
            PartNo = "A11",
            SerialNo = 453,
            Parameter = "C -11",

            // This you should add as date time,  I just used the string to parse your time that you showed in your example.
            InstallDate = DateTime.Parse("2013-02-04T17:16:56.383+05:30"),
            InstallTill = DateTime.Parse("2013-02-15T17:16:56.3830837+05:30")
        };
        return details;
    }

    private PartInfo details_2()
    {
        PartInfo details = new PartInfo
        {
            ID = 1,
            Name = "EAFR",
            PartNo = "B07",
            SerialNo = 32,
            Parameter = "B-16",

            // This you should add as date time,  I just used the string to parse your time that you showed in your example.
            InstallDate = DateTime.Parse("2013-02-18T17:17:44.589+05:30"),
            InstallTill = DateTime.Parse("2013-02-28T17:17:44.589+05:30")
        };
        return details;
    }

    public void setXmlValues()
    {            
        Documentelement testOut = new Documentelement { PartInfo = new[] { details_1(), details_2() }};

        xml_serialise(testOut);

        Documentelement testIn = xml_deserialise();
        int val = testIn.PartInfo[0].ID;
        DateTime dt = testIn.PartInfo[0].InstallDate;
        string shortTime = dt.ToShortTimeString();

    }


    private void xml_serialise(Documentelement test)
    {
        XmlSerializer ser = new XmlSerializer(typeof(Documentelement));


        using (TextWriter writer = new StreamWriter("test.xml"))
        {
            ser.Serialize(writer, test);
        }
    }

    private Documentelement xml_deserialise()
    {
        XmlSerializer ser = new XmlSerializer(typeof(Documentelement));

        Documentelement test;

        using (TextReader writer = new StreamReader("test.xml"))
        {
            test = (Documentelement)ser.Deserialize(writer);
        }

        return test;
    }
}
}

One approach could be to use xsd.exe (one of the VS command-line tools) to generate a schema from the sample XML, and then use xsd.exe with the schema to generate a C# class. This class is ready to use with serialization, so you could convert your data into a List<GeneratedClass> and then serialize it . Not saying it's the best approach, but I've used it before successfully.

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