1

我有一个基于模式文件创建示例 xml 的 API。这是 API 的链接:http: //msdn.microsoft.com/en-us/library/aa302296.aspx

现在,假设生成的示例 xml 如下:

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

我有一个包含数千条员工记录的员工数据库表。我想要的是以上面创建的 xml 格式编写这些记录(表有许多非必需的列也不能使用dataset.writexml)。

这样做的正确方法是什么?我是否应该根据第一条记录编辑第一个节点,然后将整个节点复制到相同的 xml 中,写入记录然后重复该过程直到记录结束?还是有更好的方法来做到这一点?

4

2 回答 2

1

这是将对象序列化和反序列化为 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;
    }
}
}
于 2013-06-26T10:48:15.390 回答
0

一种方法是使用xsd.exe(VS 命令行工具之一)从示例 XML 生成架构,然后xsd.exe与架构一起使用以生成 C# 类。此类已准备好与序列化一起使用,因此您可以将数据转换为 aList<GeneratedClass>然后对其进行序列化。不是说这是最好的方法,但我之前成功地使用过它。

于 2013-06-26T10:43:46.707 回答