本文介绍了如何使用动态标记名称将数组序列化为 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 C# 上使用动态标记名称将对象数组序列化为 XML.我创建了两个需要序列化为 xml 的类,但我不知道如何创建带有动态名称的标签.
I need to serialize an array of objects to XML on C# with dynamic tag names. I have created two classes that i need to serialize into a xml, but i can't figure out how to create tags with dynamic names.
例如,我有以下课程:
[System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
public class GeneralInformation
{
private Info[] addInfoList;
/// <remarks/>
[System.Xml.Serialization.XmlArray("InfoList")]
public Info[] AddInfoList
{
get
{
return this.addInfoList;
}
set
{
this.addInfoList = value;
}
}
}
public class Info
{
private string infoMessage;
/// <remarks/>
[System.Xml.Serialization.XmlElement("InfoName")]
public string InfoMessage
{
get
{
return this.infoMessage;
}
set
{
this.infoMessage = value;
}
}
}
如果我添加一些简单的数据并将其序列化,我会得到:
If I add some simple data and serialize it, I get this:
<?xml version="1.0" encoding="utf-16"?>
<GeneralInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<InfoList>
<Info>
<InfoName>Test1</InfoName>
</Info>
<Info>
<InfoName>Test2</InfoName>
</Info>
<Info>
<InfoName>Test3</InfoName>
</Info>
</InfoList>
</GeneralInformation>
但是我需要用数组的索引 + 1 来枚举标签Info".这可能吗?结果看起来像这样.
But i need to enumerate the tag "Info" with the index of the array + 1. Is that possible? The result would look like this.
<?xml version="1.0" encoding="utf-16"?>
<GeneralInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<InfoList>
<Info001>
<InfoName>Test1</InfoName>
</Info001>
<Info002>
<InfoName>Test2</InfoName>
</Info002>
<Info003>
<InfoName>Test3</InfoName>
</Info003>
</InfoList>
</GeneralInformation>
注意我只需要将我的 GeneralInformation
序列化为 XML,而不是反序列化.
Note I only need to serialize my GeneralInformation
to XML, not deserialize.
推荐答案
使用 XDocument :
Using XDocument :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication45
{
class Program
{
static void Main(string[] args)
{
string xmlIdent = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
"<GeneralInformation xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"</GeneralInformation>";
XDocument doc = XDocument.Parse(xmlIdent);
XElement generalInfo = doc.Root;
XElement infoList = new XElement("InfoList");
generalInfo.Add(infoList);
for (int i = 0; i < 10; i++)
{
infoList.Add(new XElement("Infor" + i.ToString("0##"), new XElement("InfoName", "Test" + i.ToString("0##"))));
}
}
}
}
//<?xml version="1.0" encoding="utf-16"?>
//<GeneralInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// <InfoList>
// <Info001>
// <InfoName>Test1</InfoName>
// </Info001>
// <Info002>
// <InfoName>Test2</InfoName>
// </Info002>
// <Info003>
// <InfoName>Test3</InfoName>
// </Info003>
// </InfoList>
//</GeneralInformation>
这篇关于如何使用动态标记名称将数组序列化为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!