序列化 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 的名称。最初对象项是一个字典键,值:
文件名,测试5;
目的地,0;
测试1,EVA00
test2,科。
你能帮帮我吗?
谢谢!
DataContract 在这种情况下最好。也可以看到一般differences.
尝试以下代码:
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);
}
}
}
}
}
我需要以下 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 的名称。最初对象项是一个字典键,值:
文件名,测试5; 目的地,0; 测试1,EVA00 test2,科。
你能帮帮我吗?
谢谢!
DataContract 在这种情况下最好。也可以看到一般differences.
尝试以下代码:
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);
}
}
}
}
}