繁体   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