gpt4 book ai didi

c# - 使用命名空间将类序列化为 XML

转载 作者:行者123 更新时间:2023-12-04 17:01:39 27 4
gpt4 key购买 nike

我实际上正在努力将我的类序列化为所需的 XML。我在以正确的方式放置命名空间时遇到问题。

这是所需的 XML:

<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://abc.def.schema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<q0:LoginScopeHeader>
<organizationName>WebService Test Account</organizationName>
</q0:LoginScopeHeader>
<q0:SessionHeader>
<sessionId>00f63ba748474ebba5a5ce0f8fdf7ac4</sessionId>
</q0:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<q0:GroupSet>
<staticGroup>
<number>10000</number>
<name>Gruppe A</name>
<conference>false</conference>
<activated>true</activated>
<personsCounter>0</personsCounter>
<messageName xsi:nil="true"/>
<personNumber xsi:nil="true"/>
</staticGroup>
<staticGroup>
<number>10000</number>
<name>Gruppe A</name>
<conference>false</conference>
<activated>true</activated>
<personsCounter>0</personsCounter>
<messageName xsi:nil="true"/>
<personNumber xsi:nil="true"/>
</staticGroup>
</q0:GroupSet>
</soapenv:Body>

实际上,我的类表示如下所示:
[XmlRoot(ElementName = "Envelope")]
public class Envelope
{
[XmlElement(ElementName = "Header")]
public Header Header { get; set; }
[XmlElement(ElementName = "Body")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "soapenv")]
public string Soapenv { get; set; }
[XmlAttribute(AttributeName = "q0")]
public string Q0 { get; set; }
[XmlAttribute(AttributeName = "xsd")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "xsi")]
public string Xsi { get; set; }
}

[XmlRoot(ElementName = "LoginScopeHeader")]
public class LoginScopeHeader
{
[XmlElement(ElementName = "organizationName")]
public string OrganizationName { get; set; }
}

[XmlRoot(ElementName = "SessionHeader")]
public class SessionHeader
{
[XmlElement(ElementName = "sessionId")]
public string SessionId { get; set; }
}

[XmlRoot(ElementName = "Header")]
public class Header
{
[XmlElement(ElementName = "LoginScopeHeader")]
public LoginScopeHeader LoginScopeHeader { get; set; }
[XmlElement(ElementName = "SessionHeader")]
public SessionHeader SessionHeader { get; set; }
}

[XmlRoot(ElementName = "Body")]
public class Body
{
[XmlElement(ElementName = "GroupSet")]
public GroupSet GroupSet { get; set; }
}

[XmlRoot(ElementName = "GroupSet")]
public class GroupSet
{
[XmlElement(ElementName = "staticGroup")]
public List<StaticGroup> StaticGroup { get; set; }
}

[XmlRoot(ElementName = "staticGroup")]
public class StaticGroup
{
[XmlElement(ElementName = "number")]
public string Number { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "conference")]
public string Conference { get; set; }
[XmlElement(ElementName = "activated")]
public string Activated { get; set; }
[XmlElement(ElementName = "personsCounter")]
public string PersonsCounter { get; set; }
[XmlElement(ElementName = "messageName")]
public MessageName MessageName { get; set; }
[XmlElement(ElementName = "personNumber")]
public PersonNumber PersonNumber { get; set; }
}

[XmlRoot(ElementName = "messageName")]
public class MessageName
{
[XmlAttribute(AttributeName = "nil")]
public string Nil { get; set; }
}

[XmlRoot(ElementName = "personNumber")]
public class PersonNumber
{
[XmlAttribute(AttributeName = "nil")]
public string Nil { get; set; }
}

这里是序列化的扩展方法:
public static string XmlSerialize<T>(this T item, bool removeNamespaces = true)
{
object locker = new object();

XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
xmlns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");

XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;

lock (locker)
{
StringBuilder stringBuilder = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(stringBuilder))
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
{
if (removeNamespaces)
{
xmlSerializer.Serialize(xmlWriter, item, xmlns);
}
else { xmlSerializer.Serialize(xmlWriter, item); }

return stringBuilder.ToString();
}
}
}
}

我实际上不知道如何像上面的 XML 那样序列化命名空间。

我想念什么?任何帮助表示高度赞赏

最佳答案

您需要使用适当的命名空间分配序列化属性。 Envelope、Body 和 Header 元素具有命名空间 http://schemas.xmlsoap.org/soap/envelope/ .所以你的 Envelope类应该是这样的:

[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Header")]
public Header Header { get; set; }
[XmlElement(ElementName = "Body")]
public Body Body { get; set; }
}

您已添加命名空间前缀 (q0, xsi, xsd) 作为 Envelope 的属性类,这不是必需的,因此您可以删除它们。

涉及的另一个命名空间是 http://abc.def.schema其中有 q0字首。您应该在顶层需要的地方分配它,例如,在 Body 中类,它应该分配给 GroupSet属性(property):
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "GroupSet", Namespace = "http://abc.def.schema")]
public GroupSet GroupSet { get; set; }
}

当您开始序列化时,目前您并没有告诉序列化程序有关 q0 的信息。命名空间前缀。所以你需要在你的 XmlSerialize<T> 中添加这个扩展方法:
xmlns.Add("q0", "http://abc.def.schema");

您的 StaticGroup element 没有在您的示例 XML 中定义命名空间。所以你的 GroupSet需要在这里定义一个空的命名空间:
[XmlRoot(ElementName = "GroupSet", Namespace = "http://abc.def.schema")]
public class GroupSet
{
[XmlElement(ElementName = "staticGroup", Namespace = "")]
public List<StaticGroup> StaticGroup { get; set; }
}

关于c# - 使用命名空间将类序列化为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57393122/

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