繁体   English   中英

需要帮助将 XML 转换为对象

[英]Need help in converting the XML to Objects

您好,我需要帮助将 XML 转换为对象类,因此每当我将该对象转换回 XML 格式时,我都会得到与某些 API 请求中预期的输出相同的输出。 现在我使用在线工具 ( https://json2csharp.com/code-converters/xml-to-csharp ) 将该模型转换为 XML,但仍然没有达到预期。 就像转换后根属性丢失了,即 xmlns:p 也是 <p: name 的起始标记,因此调用 API 失败,因为他们希望我一起发送

示例 XML 在这里:

<p:EIDVBusinessSearch xmlns:p="example.com" xmlns:xsi="abc.com" xsi:schemaLocation="cde.com">
  <PermissiblePurpose>
    <GLB>{{GLB}}</GLB>
    <DPPA>{{DPPA}}</DPPA>
    <VOTER>{{VOTER}}</VOTER>
 <PermissiblePurpose>
</p:EIDVBusinessSearch>.

我使用 .NET 6 框架创建了一个桌面 WinForm 应用程序并编写了以下代码。 我希望这段代码能以某种方式帮助您解决问题。

注意:您必须正确使用XmlAttribute Namespace来修复丢失的属性问题。

但是,起始p:将保持丢失状态。

EidBusinessSearch 类:

[XmlRoot(ElementName = "EIDVBusinessSearch", Namespace = "example.com")]
public class EIDVBusinessSearch
{

    [XmlElement(ElementName = "PermissiblePurpose", Namespace = "")]
    public PermissiblePurpose? PermissiblePurpose { get; set; }

    [XmlAttribute(AttributeName = "p", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string? P { get; set; }

    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string? Xsi { get; set; }

    [XmlAttribute(AttributeName = "schemaLocation", Namespace = "abc.com")]
    public string? SchemaLocation { get; set; }

    [XmlText]
    public string? Text { get; set; }
}

许可用途

[XmlRoot(ElementName = "PermissiblePurpose", Namespace = "")]
public class PermissiblePurpose
{

    [XmlElement(ElementName = "GLB", Namespace = "")]
    public string? GLB { get; set; }

    [XmlElement(ElementName = "DPPA", Namespace = "")]
    public string? DPPA { get; set; }

    [XmlElement(ElementName = "VOTER", Namespace = "")]
    public string? VOTER { get; set; }
}

将 XML 转换为对象的方法:

private void ConvertXMLtoObject()
    {
        try
        {
            string xml = "<p:EIDVBusinessSearch xmlns:p=\"example.com\" xmlns:xsi=\"abc.com\" xsi:schemaLocation=\"cde.com\">" +
                "\r\n  <PermissiblePurpose>" +
                "\r\n    <GLB>{{GLB}}</GLB>" +
                "\r\n    <DPPA>{{DPPA}}</DPPA>" +
                "\r\n    <VOTER>{{VOTER}}</VOTER>" +
                "\r\n </PermissiblePurpose>" +
                "\r\n</p:EIDVBusinessSearch>";

            XmlSerializer serializer = new(typeof(EIDVBusinessSearch));

            EIDVBusinessSearch obj = new();

            using StringReader reader = new(xml);
            obj = (EIDVBusinessSearch)serializer.Deserialize(reader);


        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Application.Exit();
        }
    }

将对象转换为 XML:

private void ConvertObjectToXML()
    {
        try
        {
            EIDVBusinessSearch obj = new()
            {
                P = "example.com",
                PermissiblePurpose = new()
            };
            obj.PermissiblePurpose.GLB = "GLB";
            obj.PermissiblePurpose.DPPA = "DPPA";
            obj.PermissiblePurpose.VOTER = "VOTER";

            XmlSerializer serializer = new(obj.GetType());

            using StringWriter writer = new();
            serializer.Serialize(writer, obj);
            string xml = writer.ToString();

            label1.Text = xml;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Application.Exit();
        }
    }

输出:

输出:XML 字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM