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>

何千もの従業員レコードで構成される従業員 DB テーブルがあります。私が望むのは、これらのレコードを上で作成した 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

1 つの方法xsd.exeとして、(VS コマンド ライン ツールの 1 つ) を使用してサンプル XML からxsd.exeスキーマを生成し、そのスキーマを使用して C# クラスを生成することができます。このクラスはシリアル化ですぐに使用できるため、データを に変換してからList<GeneratedClass>シリアルできます。それが最善のアプローチだと言っているわけではありませんが、以前は成功していました。

于 2013-06-26T10:43:46.707 に答える