好的。首先,您应该为 xml 模式生成 XSD 文件 - 对于从 xml 生成类很有用。这是您的示例 .xsd 文件
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="message" type="message"/>
<xs:complexType name="message">
<xs:sequence>
<xs:element name="attachmentName" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="messageBody" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="primaryRecipient" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="secodaryRecipient" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="sender" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="subject" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:element name="message" type="message" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
第二件事是从 .xsd 文件生成 C# 和 Java 类。您可以这样做: 1. 从CMD调用 Java 类 2. 从 Visual Studio 命令行xjc -p <package> <path_to_xsd_file.xsd>
调用 C# 类
xsd /C <path_to_xsd_file.xsd>
将您的 java 和 c# 类附加到您的项目中。生成的类应具有C# 和 Java 中的名称root
和。message
Root
Message
要序列化和解析xml,您可以使用如下代码: 1. 在 C# 端 - 序列化为 xml(字符串/字节 [] <- 取决于对您有用的内容)
MemoryStream stream = new MemoryStream();
root objectRoot = new root();
objectRoot.message = new message[2];
objectRoot.message[0] = new message();
objectRoot.message[0].attachmentName = "msg1";
objectRoot.message[0].messageBody = "mb1";
objectRoot.message[0].primaryRecipient = "pr1";
objectRoot.message[0].secodaryRecipient = "sr1";
objectRoot.message[0].sender = "s1";
objectRoot.message[0].subject = "su1";
objectRoot.message[1] = new message();
objectRoot.message[1].attachmentName = "msg2";
objectRoot.message[1].messageBody = "mb2";
objectRoot.message[1].primaryRecipient = "pr2";
objectRoot.message[1].secodaryRecipient = "sr2";
objectRoot.message[1].sender = "s2";
objectRoot.message[1].subject = "su2";
XmlSerializer serializer = new XmlSerializer(typeof(root));
serializer.Serialize(stream, objectRoot);
byte[] toSend = stream.ToArray();`
2.在java端
byte[] requestByte;
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Root finalizationParameters = (Root)
jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(requestByte));
我希望它会有所帮助。