[英]Convert JSON to SOAP XML with predefined namespaces
I am given task to convert SOAP request to ReST JSON and then JSON back to SOAP XML. 我已经完成了使用 Serialize 和 XMlElement() 类将 SOAP 转换为 JSON 的部分。 现在最棘手的部分是我需要将 JSON 转换为 SOAP。 有我们自己编写的命名空间,我想在将 JSON 转换为 SOAP XML 时将这些命名空间放回去。 例如,
<asr:APIRequest
xmlns:guid-"http://Some-test-namespace.xsd" xmlns:xsi="https://www.w3.org/2001/XMLSchema" xmins:asw="http://Some-test-namespace.xsd" xmIns:ar="http://Some-test-namespace.xsd" xmlns:asr="http://Some-test-namespace.xsd">
<asr:APIRequestDetails>
<ar:Individual>
<ar:IndividualKey>3791239123-123123123-1231231</ar:IndividualKey>
</ar:Individual>
</asr:APIRequestDetails>
</asr:APIRequest>
我将其转换为 JSON
{
"APIRequest": {
"APIRequestDetails": {
"Individual": {
"IndividualKey": "3791239123-123123123-1231231"
}
}
}
}
现在我想将其转换回 SOAP XML 并附加命名空间,例如“asr”、“ar”。 我正在使用 C#.Net Core 并使用 XDocument、XMLSerializer 将 XML 转换为 JSON。
任何人都可以帮我将 JSON 转换回 SOAP XML 以及NAMESPACES吗?
除非使用,否则不会添加命名空间。 尝试关注
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication40
{
class Program
{
const string INPUT_FILENAME = @"c:\temp\test.xml";
const string OUTPUT_FILENAME = @"c:\temp\test1.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(INPUT_FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(APIRequest));
APIRequest request = (APIRequest)serializer.Deserialize(reader);
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("guid", "http://Some-test-namespace.xsd");
namespaces.Add("xsi", "https://www.w3.org/2001/XMLSchema");
namespaces.Add("asw", "http://Some-test-namespace.xsd");
namespaces.Add("ar", "http://Some-test-namespace.xsd");
namespaces.Add("asr", "http://Some-test-namespace.xsd");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(OUTPUT_FILENAME,settings);
serializer.Serialize(writer, request, namespaces);
}
}
[XmlRoot(Namespace = "http://Some-test-namespace.xsd")]
public class APIRequest
{
[XmlElement(Namespace = "http://Some-test-namespace.xsd")]
public APIRequestDetails APIRequestDetails { get; set; }
}
public class APIRequestDetails
{
[XmlElement(Namespace = "http://Some-test-namespace.xsd")]
public Individual Individual { get; set; }
}
public class Individual
{
[XmlElement(Namespace = "http://Some-test-namespace.xsd")]
public string IndividualKey { get; set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.