【问题标题】:C# .Net - write data in xml by using existing structure/schemaC# .Net - 使用现有结构/模式在 xml 中写入数据
【发布时间】:2013-06-26 10:36:56
【问题描述】:

我有一个基于模式文件创建示例 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 中,写入记录然后重复该过程直到记录结束?或者有更好的方法吗?

【问题讨论】:

  • 你有没有尝试过?您需要显示您尝试过的代码并指定更具体的问题。
  • 如果你创建了一个匹配数据模型的对象,比如一个员工类包含所有员工细节作为属性,并且它实现了IEnumerable,那么你可以将这个类序列化/反序列化为xml。跨度>
  • @Jegan 不能那样做。因为我不知道类的属性是什么。字段由 xsd 文件在运行时确定。
  • 然后使用字典或元组在运行时接受一个字段和值,将其解析为序列化程序。

标签: c# .net xml


【解决方案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;
    }
}
}

【讨论】:

    【解决方案2】:

    一种方法是使用 xsd.exe(VS 命令行工具之一)从示例 XML 生成架构,然后使用 xsd.exe 和架构生成 C# 类。此类已准备好与序列化一起使用,因此您可以将数据转换为List&lt;GeneratedClass&gt;,然后再转换为serialize it。不是说这是最好的方法,但我之前成功使用过。

    【讨论】:

    • 我最初想到了这种方法。但是 xsd 是在运行时提供给程序的。因此,这种方法将涉及在运行时调用 xsd.exe,即时编译类,然后使用反射将其序列化为 xml。我猜有点乏味且容易出错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    相关资源
    最近更新 更多