如何序列化 属性 名称和 属性 值作为属性值
How to serialize property name and property value as attribute value
我的 class 看起来像
public class Test
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
我想要 xml 这样的结果:
<NodeList>
<Node>
<DataNode Key="Name" Value="Tom" />
<DataNode Key="Age" Value="30" />
</Node>
<Node>
<DataNode Key="Name" Value="John" />
<DataNode Key="Age" Value="35" />
</Node>
</NodeList>
我试过在属性中设置XmlAttribute,但结果不是我想要的。有什么建议吗?
更新:
这是我得到的:
<?xml version="1.0" encoding="utf-16"?><Node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="Allen" Age="28" />
我建议您对数据使用更直接的方法,例如:
[Serializable()]
public class NodeList {
[XmlArray("Node")]
[XmlArrayItem("DataNode")]
public Test[] Nodes;
}
public class Test {
[XmlAttribute]
public string Name { get; set; }
public int Age { get; set; }
}
并像这样使用它:
string folderpath = Application.StartupPath + "\settings";
string appSettingsFilename = "testsettings2";
if (!Directory.Exists(folderpath))
Directory.CreateDirectory(folderpath);
string filepath = folderpath + "\" + appSettingsFilename + ".xml";
NodeList nodes = new NodeList();
XmlSerializer serializer = new XmlSerializer(typeof(NodeList));
TextWriter configWriteFileStream = new StreamWriter(filepath);
nodes.Nodes = new Test[2] {
new Test() { Name = "Tom", Age=30},
new Test() { Name = "John", Age=35}
};
serializer.Serialize(configWriteFileStream, nodes);
configWriteFileStream.Close();
你将得到:
<?xml version="1.0" encoding="utf-8"?>
<NodeList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node>
<DataNode Name="Tom" Age="30" />
<DataNode Name="John" Age="35" />
</Node>
</NodeList>
话虽这么说,要获得你想要的 XML 文件,你实际上应该像这样声明你的 类(注释):
[Serializable()]
public class DummyClass2 {
[XmlElement("NodeList")] //necessary to indicate that this is an element, otherwise will be considered as array
public TestList[] NodeList = null;
}
public class TestList {
[XmlArray("Node")] //let this be array
[XmlArrayItem("DataNode")]
public Test[] TestItem { get; set; }
}
public class Test {
private string key;
[XmlAttribute("Key")]
public string Key { //declare as Key instead
get { return key; }
set { key = value; }
}
private string value2; //cannot be int, must be string to accommodate both "Tom" and "30"
[XmlAttribute("Value")]
public string Value { //declare as Value instead
get { return value2; }
set { value2 = value; }
}
}
然后你这样使用它:
string folderpath = Application.StartupPath + "\settings";
string appSettingsFilename = "testsettings";
if (!Directory.Exists(folderpath))
Directory.CreateDirectory(folderpath);
string filepath = folderpath + "\" + appSettingsFilename + ".xml";
DummyClass2 dummyClass2 = new DummyClass2();
XmlSerializer serializer = new XmlSerializer(typeof(DummyClass2));
TextWriter configWriteFileStream = new StreamWriter(filepath);
dummyClass2.NodeList = new TestList[2] {
new TestList() {
TestItem = new Test[2] {
new Test() { Key="Name", Value="Tom"},
new Test() { Key="Age", Value="30"}
}
},
new TestList() {
TestItem = new Test[2] {
new Test() { Key="Name", Value="John"},
new Test() { Key="Age", Value="35"}
}
}
};
serializer.Serialize(configWriteFileStream, dummyClass2);
configWriteFileStream.Close();
你应该得到:
<?xml version="1.0" encoding="utf-8"?>
<DummyClass2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NodeList>
<Node>
<DataNode Key="Name" Value="Tom" />
<DataNode Key="Age" Value="30" />
</Node>
</NodeList>
<NodeList>
<Node>
<DataNode Key="Name" Value="John" />
<DataNode Key="Age" Value="35" />
</Node>
</NodeList>
</DummyClass2>
您不需要序列化。试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication85
{
class Program
{
static void Main(string[] args)
{
var inputs = new[] {
new { name = "Tom", age = 30},
new { name = "John", age = 35}
};
XElement nodeList = new XElement("NodeList");
XElement node = new XElement("Node");
nodeList.Add(node);
foreach (var input in inputs)
{
node.Add(new XElement("DataNode", new XAttribute[] { new XAttribute("Key", input.name), new XAttribute("Value", input.age)}));
}
}
}
}
我的 class 看起来像
public class Test
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
我想要 xml 这样的结果:
<NodeList>
<Node>
<DataNode Key="Name" Value="Tom" />
<DataNode Key="Age" Value="30" />
</Node>
<Node>
<DataNode Key="Name" Value="John" />
<DataNode Key="Age" Value="35" />
</Node>
</NodeList>
我试过在属性中设置XmlAttribute,但结果不是我想要的。有什么建议吗?
更新: 这是我得到的:
<?xml version="1.0" encoding="utf-16"?><Node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="Allen" Age="28" />
我建议您对数据使用更直接的方法,例如:
[Serializable()]
public class NodeList {
[XmlArray("Node")]
[XmlArrayItem("DataNode")]
public Test[] Nodes;
}
public class Test {
[XmlAttribute]
public string Name { get; set; }
public int Age { get; set; }
}
并像这样使用它:
string folderpath = Application.StartupPath + "\settings";
string appSettingsFilename = "testsettings2";
if (!Directory.Exists(folderpath))
Directory.CreateDirectory(folderpath);
string filepath = folderpath + "\" + appSettingsFilename + ".xml";
NodeList nodes = new NodeList();
XmlSerializer serializer = new XmlSerializer(typeof(NodeList));
TextWriter configWriteFileStream = new StreamWriter(filepath);
nodes.Nodes = new Test[2] {
new Test() { Name = "Tom", Age=30},
new Test() { Name = "John", Age=35}
};
serializer.Serialize(configWriteFileStream, nodes);
configWriteFileStream.Close();
你将得到:
<?xml version="1.0" encoding="utf-8"?>
<NodeList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node>
<DataNode Name="Tom" Age="30" />
<DataNode Name="John" Age="35" />
</Node>
</NodeList>
话虽这么说,要获得你想要的 XML 文件,你实际上应该像这样声明你的 类(注释):
[Serializable()]
public class DummyClass2 {
[XmlElement("NodeList")] //necessary to indicate that this is an element, otherwise will be considered as array
public TestList[] NodeList = null;
}
public class TestList {
[XmlArray("Node")] //let this be array
[XmlArrayItem("DataNode")]
public Test[] TestItem { get; set; }
}
public class Test {
private string key;
[XmlAttribute("Key")]
public string Key { //declare as Key instead
get { return key; }
set { key = value; }
}
private string value2; //cannot be int, must be string to accommodate both "Tom" and "30"
[XmlAttribute("Value")]
public string Value { //declare as Value instead
get { return value2; }
set { value2 = value; }
}
}
然后你这样使用它:
string folderpath = Application.StartupPath + "\settings";
string appSettingsFilename = "testsettings";
if (!Directory.Exists(folderpath))
Directory.CreateDirectory(folderpath);
string filepath = folderpath + "\" + appSettingsFilename + ".xml";
DummyClass2 dummyClass2 = new DummyClass2();
XmlSerializer serializer = new XmlSerializer(typeof(DummyClass2));
TextWriter configWriteFileStream = new StreamWriter(filepath);
dummyClass2.NodeList = new TestList[2] {
new TestList() {
TestItem = new Test[2] {
new Test() { Key="Name", Value="Tom"},
new Test() { Key="Age", Value="30"}
}
},
new TestList() {
TestItem = new Test[2] {
new Test() { Key="Name", Value="John"},
new Test() { Key="Age", Value="35"}
}
}
};
serializer.Serialize(configWriteFileStream, dummyClass2);
configWriteFileStream.Close();
你应该得到:
<?xml version="1.0" encoding="utf-8"?>
<DummyClass2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NodeList>
<Node>
<DataNode Key="Name" Value="Tom" />
<DataNode Key="Age" Value="30" />
</Node>
</NodeList>
<NodeList>
<Node>
<DataNode Key="Name" Value="John" />
<DataNode Key="Age" Value="35" />
</Node>
</NodeList>
</DummyClass2>
您不需要序列化。试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication85
{
class Program
{
static void Main(string[] args)
{
var inputs = new[] {
new { name = "Tom", age = 30},
new { name = "John", age = 35}
};
XElement nodeList = new XElement("NodeList");
XElement node = new XElement("Node");
nodeList.Add(node);
foreach (var input in inputs)
{
node.Add(new XElement("DataNode", new XAttribute[] { new XAttribute("Key", input.name), new XAttribute("Value", input.age)}));
}
}
}
}