gpt4 book ai didi

c# - 从 XmlSerializer 生成 Xml 时如何控制 Xml 文档中字符串项数组的 XmlElement 名称

转载 作者:行者123 更新时间:2023-12-03 09:20:49 24 4
gpt4 key购买 nike

我正在使用 XmlSerializer 类将对象序列化为 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><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa899f949e9f88ba9f979b9396d4939e" rel="noreferrer noopener nofollow">[email protected]</a></SenderEmailId>
<ToRecipientEmailId>
<string><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fdbe0cfeae2eee6e3a1e6eb" rel="noreferrer noopener nofollow">[email protected]</a></string>
<string><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="da8eb5eb9abfb7bbb3b6f4b3be" rel="noreferrer noopener nofollow">[email protected]</a></string>
<string><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81d5eeb3c1e4ece0e8edafe8e5" rel="noreferrer noopener nofollow">[email protected]</a></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><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5a6b0bbb1b0a795b0b8b4bcb9fbbcb1" rel="noreferrer noopener nofollow">[email protected]</a></SenderEmailId>
<ToRecipientEmailId>
<EmailId><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b1f240b2e262a222765222f" rel="noreferrer noopener nofollow">[email protected]</a></EmailId>
<EmailId><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43172c7203262e222a2f6d2a27" rel="noreferrer noopener nofollow">[email protected]</a></EmailId>
<EmailId><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d480bbe694b1b9b5bdb8fabdb0" rel="noreferrer noopener nofollow">[email protected]</a></EmailId>
</ToRecipientEmailId>
</MailItem>

当我创建另一个类并将 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><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8dbcdc6cccddae8cdc5c9c1c486c1cc" rel="noreferrer noopener nofollow">[email protected]</a></SenderEmailId>
<ToRecipientEmailId>
<Email>
<EmailID><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c48735c79717d7570327578" rel="noreferrer noopener nofollow">[email protected]</a></EmailID>
</Email>
<Email>
<EmailID><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="194d7628597c7478707537707d" rel="noreferrer noopener nofollow">[email protected]</a></EmailID>
</Email>
<Email>
<EmailID><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="da8eb5e89abfb7bbb3b6f4b3be" rel="noreferrer noopener nofollow">[email protected]</a></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] 属性。

关于c# - 从 XmlSerializer 生成 Xml 时如何控制 Xml 文档中字符串项数组的 XmlElement 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com