从 XmlSerializer 生成 Xml 时,如何控制 Xml 文档中字符串项数组的 Xml 元素名称
How to control XmlElement name for array of string items in Xml document when generating Xml from XmlSerializer
我正在使用 XmlSerializer class 将对象序列化为 xml 文件并通过网络传输。
我创建了以下数据结构,其中包含数据并通过电线传输。
[Serializable]
public class MailItem
{
public string UniqueEmailId { get; set; }
public string SenderEmailId { get; set; }
public string[] ToRecipientEmailId { get; set; }
}
现在,当我转换为 xml 时,它会发出以下结果 –
<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UniqueEmailId>SomeUniqueID</UniqueEmailId>
<SenderEmailId>sender@email.id</SenderEmailId>
<ToRecipientEmailId>
<string>To@email.id</string>
<string>To1@email.id</string>
<string>To2@email.id</string>
</ToRecipientEmailId>
</MailItem>
预期结果是(观察 ToRecipientEmailId 的子元素)–
<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UniqueEmailId>SomeUniqueID</UniqueEmailId>
<SenderEmailId>sender@email.id</SenderEmailId>
<ToRecipientEmailId>
<EmailId>To@email.id</EmailId>
<EmailId>To1@email.id</EmailId>
<EmailId>To2@email.id</EmailId>
</ToRecipientEmailId>
</MailItem>
当我创建另一个 class 并将 Email 和 EmailId 作为 属性 放入其中时,它给了我以下结果 –
[Serializable]
public class MailItem
{
public string UniqueEmailId { get; set; }
public string SenderEmailId { get; set; }
public List<Email> ToRecipientEmailId { get; set; }
}
public class Email
{
public string EmailID {get;set;}
}
<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UniqueEmailId>SomeUniqueID</UniqueEmailId>
<SenderEmailId>sender@email.id</SenderEmailId>
<ToRecipientEmailId>
<Email>
<EmailID>To@email.id</EmailID>
</Email>
<Email>
<EmailID>To1@email.id</EmailID>
</Email>
<Email>
<EmailID>To2@email.id</EmailID>
</Email>
</ToRecipientEmailId>
</MailItem>
如果有人告诉我如何达到预期的结果,那就太好了。我需要对数据结构进行哪些更改?
您可以使用 [XmlArrayItem(elementName)]
属性来指定要用于数组条目的元素名称:
public class MailItem
{
public string UniqueEmailId { get; set; }
public string SenderEmailId { get; set; }
[XmlArrayItem("EmailId")]
public string[] ToRecipientEmailId { get; set; }
}
请注意,您不需要用于二进制序列化的 [Serializable]
属性。
试试这个。不要使用 XmlArrayItem 会创建一个额外的标签层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
[XmlRoot("MailItem")]
public class MailItem
{
[XmlElement("UniqueEmailId")]
public string UniqueEmailId { get; set; }
[XmlElement("SenderEmailId")]
public string SenderEmailId { get; set; }
[XmlElement("ToRecipientEmailId")]
public string[] ToRecipientEmailId { get; set; }
}
}
我正在使用 XmlSerializer class 将对象序列化为 xml 文件并通过网络传输。 我创建了以下数据结构,其中包含数据并通过电线传输。
[Serializable]
public class MailItem
{
public string UniqueEmailId { get; set; }
public string SenderEmailId { get; set; }
public string[] ToRecipientEmailId { get; set; }
}
现在,当我转换为 xml 时,它会发出以下结果 –
<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UniqueEmailId>SomeUniqueID</UniqueEmailId>
<SenderEmailId>sender@email.id</SenderEmailId>
<ToRecipientEmailId>
<string>To@email.id</string>
<string>To1@email.id</string>
<string>To2@email.id</string>
</ToRecipientEmailId>
</MailItem>
预期结果是(观察 ToRecipientEmailId 的子元素)–
<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UniqueEmailId>SomeUniqueID</UniqueEmailId>
<SenderEmailId>sender@email.id</SenderEmailId>
<ToRecipientEmailId>
<EmailId>To@email.id</EmailId>
<EmailId>To1@email.id</EmailId>
<EmailId>To2@email.id</EmailId>
</ToRecipientEmailId>
</MailItem>
当我创建另一个 class 并将 Email 和 EmailId 作为 属性 放入其中时,它给了我以下结果 –
[Serializable]
public class MailItem
{
public string UniqueEmailId { get; set; }
public string SenderEmailId { get; set; }
public List<Email> ToRecipientEmailId { get; set; }
}
public class Email
{
public string EmailID {get;set;}
}
<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UniqueEmailId>SomeUniqueID</UniqueEmailId>
<SenderEmailId>sender@email.id</SenderEmailId>
<ToRecipientEmailId>
<Email>
<EmailID>To@email.id</EmailID>
</Email>
<Email>
<EmailID>To1@email.id</EmailID>
</Email>
<Email>
<EmailID>To2@email.id</EmailID>
</Email>
</ToRecipientEmailId>
</MailItem>
如果有人告诉我如何达到预期的结果,那就太好了。我需要对数据结构进行哪些更改?
您可以使用 [XmlArrayItem(elementName)]
属性来指定要用于数组条目的元素名称:
public class MailItem
{
public string UniqueEmailId { get; set; }
public string SenderEmailId { get; set; }
[XmlArrayItem("EmailId")]
public string[] ToRecipientEmailId { get; set; }
}
请注意,您不需要用于二进制序列化的 [Serializable]
属性。
试试这个。不要使用 XmlArrayItem 会创建一个额外的标签层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
[XmlRoot("MailItem")]
public class MailItem
{
[XmlElement("UniqueEmailId")]
public string UniqueEmailId { get; set; }
[XmlElement("SenderEmailId")]
public string SenderEmailId { get; set; }
[XmlElement("ToRecipientEmailId")]
public string[] ToRecipientEmailId { get; set; }
}
}