簡體   English   中英

序列化XML c#

[英]Serialization XML c#

我需要以下xml結構:

<?xml version="1.0" dateprodstart="20180319" heureprodstart="12:08:36" 
dateprodend="20180319" heureprodend="12:12:45" version="1.21" encoding="utf- 8"?>

<ListItems>

<item>
    <filename>test5</filename>
    <destination>O</destination>
    <test1>EVA00</test1>
    <test2>ko</test2>
</item>

<item>
    <filename>test</filename>
    <destination>O</destination>
    <test1>xxxx</test1>
    <test2>xxxx</test2>
</item>

...

</ListItems>

我的對象項目具有字段(已排序):文件名,目標,test1,test2。 我需要物品清單。

最好的方法是什么? 是數據合同還是XmlSerialization? 因為我需要自定義List的名稱和nodelement的名稱。 最初,對象項是字典鍵,值:

文件名test5; 目的地,0; test1,EVA00 test2,ko。

你可以幫幫我嗎?

謝謝!

在這種情況下,最好使用DataContract。 您還可以看到一般差異

嘗試以下代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string version = "1.21";
            string dateprodstart =   DateTime.Now.ToString("yyyyMMdd");
            string heureprodstart=  DateTime.Now.ToString("hh:mm:ss");
            string dateprodend =   DateTime.Now.ToString("yyyyMMdd");
            string heureprodend =  DateTime.Now.ToString("hh:mm:ss");


            Dictionary<string,List<string>> dict = new Dictionary<string,List<string>>() {
                { "Item", new List<string>() {
                            "filename, test5; destination,0; test1, EVA00; test2, ko",
                            "filename, test5; destination,0; test1, EVA00; test2, ko",
                            "filename, test5; destination,0; test1, EVA00; test2, ko",
                            "filename, test5; destination,0; test1, EVA00; test2, ko"
                          }
                }
            };
            string ident = "<?xml version=\"1.0\" encoding=\"utf- 8\"?><ListItems></ListItems>";

            XDocument doc = XDocument.Parse(ident);
            XElement listItems = doc.Root;

            listItems.Add(new XAttribute[] {
                new XAttribute("dateprodstart",dateprodstart),
                new XAttribute("heureprodstart",heureprodstart ),
                new XAttribute("dateprodend",dateprodend),
                new XAttribute("heureprodend",heureprodend),
                new XAttribute("version",version)
            });

            foreach (string item in dict["Item"].AsEnumerable())
            {
                XElement xItem = new XElement("item");
                listItems.Add(xItem);

                string[] elements = item.Split(new char[] { ';' });
                foreach (string element in elements)
                {

                    string[] csv = element.Split(new char[] { ',' });
                    XElement newElement = new XElement(csv[0].Trim(), csv[1].Trim());
                    xItem.Add(newElement);
                }
            }

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM