XML 反序列化 C# 的问题

发布于 2024-09-02 08:32:34 字数 3298 浏览 8 评论 0原文

我在 XML 反序列化方面遇到问题。

简而言之 - 我有 2 个类:

  • SMSMessage
  • SMSSendingResponse

我调用一个 API,它接受一堆参数(由 SMSMessage 类表示),

它返回一个 XML 响应。 响应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<data>
  <status>1</status>
  <message>OK</message>
  <results>
    <result>
      <account>12345</account>
      <to>012345678</to>
      <from>054321</from>
      <message>Testing</message>
      <flash></flash>
      <replace></replace>
      <report></report>
      <concat></concat>
      <id>f8d3eea1cbf6771a4bb02af3fb15253e</id>
    </result>
  </results>
</data>

这是 SMSMessage 类(到目前为止具有 xml 序列化属性)

using System.Xml.Serialization;

namespace XMLSerializationHelp
{
    [XmlRoot("results")]
    public class SMSMessage
    {
        public string To
        {
            get
            {
                return Result.To;
            }
        }

        public string From
        {
            get
            {
                return Result.From;
            }
        }

        public string Message
        {
            get
            {
                return Result.Message;
            }
        }

        [XmlElement("result")]
        public Result Result { get; set; }
    }
}

这是 SMSMessageSendingResponse:

using System.Xml.Serialization;

namespace XMLSerializationHelp
{
    [XmlRoot("data")]
    public class SMSSendingResponse
    {
        //should come from the results/result/account element. in our example "12345"
        public string AccountNumber
        {
            get
            {
                return SMSMessage.Result.AccountNumber;
            }
        }

        //should come from the "status" xml element
        [XmlElement("status")]
        public string Status { get; set; }

        //should come from the "message" xml element (in our example - "OK")
        [XmlElement("message")]
        public string Message { get; set; }

        //should come from the "id" xml element (in our example - "f8d3eea1cbf6771a4bb02af3fb15253e")
        public string ResponseID
        {
            get
            {
                return SMSMessage.Result.ResponseID;
            }
        }

        //should be created from the results/result element - ignore flash, replace, report and concat elements for now.
        [XmlElement("results")]
        public SMSMessage SMSMessage { get; set; }
    }
}

这是另一个类(结果) - 我想摆脱它,所以只前面提到的 2 个类仍然存在,

using System.Xml.Serialization;

namespace XMLSerializationHelp
{
    [XmlRoot("result")]
    public class Result
    {
        [XmlElement("account")]
        public string AccountNumber{ get; set; }

        [XmlElement("to")]
        public string To { get; set; }

        [XmlElement("from")]
        public string From { get; set; }

        [XmlElement("message")]
        public string Message { get; set; }

        [XmlElement("id")]
        public string ResponseID { get; set; }
    }
}

我不希望 SMSMessage 知道 SMSSendingResponse - 因为这将由我的应用程序的不同部分处理

I am having trouble with XML deserialization.

In a nutshell -
I have 2 classes:

  • SMSMessage
  • SMSSendingResponse

I call an API that takes a bunch of parameters (represented by SMSMessage class)

It returns an XML response.
The response looks like this:

<?xml version="1.0" encoding="utf-8"?>
<data>
  <status>1</status>
  <message>OK</message>
  <results>
    <result>
      <account>12345</account>
      <to>012345678</to>
      <from>054321</from>
      <message>Testing</message>
      <flash></flash>
      <replace></replace>
      <report></report>
      <concat></concat>
      <id>f8d3eea1cbf6771a4bb02af3fb15253e</id>
    </result>
  </results>
</data>

Here is the SMSMessage class (with the xml serialization attributes so far)

using System.Xml.Serialization;

namespace XMLSerializationHelp
{
    [XmlRoot("results")]
    public class SMSMessage
    {
        public string To
        {
            get
            {
                return Result.To;
            }
        }

        public string From
        {
            get
            {
                return Result.From;
            }
        }

        public string Message
        {
            get
            {
                return Result.Message;
            }
        }

        [XmlElement("result")]
        public Result Result { get; set; }
    }
}

Here is SMSMessageSendingResponse:

using System.Xml.Serialization;

namespace XMLSerializationHelp
{
    [XmlRoot("data")]
    public class SMSSendingResponse
    {
        //should come from the results/result/account element. in our example "12345"
        public string AccountNumber
        {
            get
            {
                return SMSMessage.Result.AccountNumber;
            }
        }

        //should come from the "status" xml element
        [XmlElement("status")]
        public string Status { get; set; }

        //should come from the "message" xml element (in our example - "OK")
        [XmlElement("message")]
        public string Message { get; set; }

        //should come from the "id" xml element (in our example - "f8d3eea1cbf6771a4bb02af3fb15253e")
        public string ResponseID
        {
            get
            {
                return SMSMessage.Result.ResponseID;
            }
        }

        //should be created from the results/result element - ignore flash, replace, report and concat elements for now.
        [XmlElement("results")]
        public SMSMessage SMSMessage { get; set; }
    }
}

Here is the other class (Result) - I want to get rid of this, so only the 2 previously mentioned classes remain

using System.Xml.Serialization;

namespace XMLSerializationHelp
{
    [XmlRoot("result")]
    public class Result
    {
        [XmlElement("account")]
        public string AccountNumber{ get; set; }

        [XmlElement("to")]
        public string To { get; set; }

        [XmlElement("from")]
        public string From { get; set; }

        [XmlElement("message")]
        public string Message { get; set; }

        [XmlElement("id")]
        public string ResponseID { get; set; }
    }
}

I don't want SMSMessage to be aware of the SMSSendingResponse - as this will be handled by a different part of my application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-09-09 08:32:34

我希望这有帮助。 XML 结构意味着 元素可以出现多次,因此看看这是否可以帮助您实现所需的效果:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;

namespace XMLSerializationHelp
{
    class Program
    {
        static void Main(string[] args)
        {
            string strXML = @"<?xml version=""1.0"" encoding=""utf-8""?>
<data>
  <status>1</status>
  <message>OK</message>
  <results>
    <result>
      <account>12345</account>
      <to>012345678</to>
      <from>054321</from>
      <message>Testing</message>
      <flash></flash>
      <replace></replace>
      <report></report>
      <concat></concat>
      <id>f8d3eea1cbf6771a4bb02af3fb15253e</id>
    </result>
  </results>
</data>";

            XmlSerializer serializer = new XmlSerializer(typeof(SMSSendingResponse));
            SMSSendingResponse obj = (SMSSendingResponse)serializer.Deserialize(new XmlTextReader(strXML, XmlNodeType.Document, null));

            Console.WriteLine("Status:         {0}", obj.Status);
            Console.WriteLine("Message:        {0}", obj.Message);
            Console.WriteLine("Account Number: {0}", obj.AccountNumber);
            Console.WriteLine("ResponseID:     {0}", obj.ResponseID);
            Console.WriteLine("To:             {0}", obj.To);
            Console.WriteLine("From:           {0}", obj.From);
            Console.WriteLine("ResultMessage:  {0}", obj.ResultMessage);

            Console.ReadLine();
        }
    }

    [Serializable]
    [XmlRoot("data")]
    public class SMSSendingResponse
    {
        public SMSSendingResponse() {}

        //should come from the "status" xml element
        [XmlElement("status")]
        public string Status { get; set; }

        //should come from the "message" xml element (in our example - "OK")
        [XmlElement("message")]
        public string Message { get; set; }

        //should come from the results/result/account element. in our example "12345"
        [XmlIgnore()]
        public string AccountNumber
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.AccountNumber : null;
            }
        }

        //should come from the "id" xml element (in our example - "f8d3eea1cbf6771a4bb02af3fb15253e")
        [XmlIgnore()]
        public string ResponseID
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.ResponseID : null; 
            }
        }

        [XmlIgnore()]
        public string To
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.To : null;
            }
        }

        [XmlIgnore()]
        public string From
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.From : null;
            }
        }

        [XmlIgnore()]
        public string ResultMessage
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.Message : null;
            }
        }

        [XmlArray("results"), XmlArrayItem("result", typeof(Result))]
        public List<Result> Results
        {
            get { return (_Results); }
            set { _Results = value; }
        } private List<Result> _Results = new List<Result>();

        [XmlIgnore()]
        public Result FirstResult
        {
            get
            {
                return (_Results != null && _Results.Count > 0) ? _Results[0] : null;
            }
        }

    }

    [XmlType(TypeName = "result"), Serializable]
    public class Result
    {
        public Result() {}

        [XmlElement("account")]
        public string AccountNumber { get; set; }

        [XmlElement("to")]
        public string To { get; set; }

        [XmlElement("from")]
        public string From { get; set; }

        [XmlElement("message")]
        public string Message { get; set; }

        [XmlElement("id")]
        public string ResponseID { get; set; }
    }
}

I hope this helps. The XML structure implies the <result> element can occur more than once, so see if this helps you achieve what you need:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;

namespace XMLSerializationHelp
{
    class Program
    {
        static void Main(string[] args)
        {
            string strXML = @"<?xml version=""1.0"" encoding=""utf-8""?>
<data>
  <status>1</status>
  <message>OK</message>
  <results>
    <result>
      <account>12345</account>
      <to>012345678</to>
      <from>054321</from>
      <message>Testing</message>
      <flash></flash>
      <replace></replace>
      <report></report>
      <concat></concat>
      <id>f8d3eea1cbf6771a4bb02af3fb15253e</id>
    </result>
  </results>
</data>";

            XmlSerializer serializer = new XmlSerializer(typeof(SMSSendingResponse));
            SMSSendingResponse obj = (SMSSendingResponse)serializer.Deserialize(new XmlTextReader(strXML, XmlNodeType.Document, null));

            Console.WriteLine("Status:         {0}", obj.Status);
            Console.WriteLine("Message:        {0}", obj.Message);
            Console.WriteLine("Account Number: {0}", obj.AccountNumber);
            Console.WriteLine("ResponseID:     {0}", obj.ResponseID);
            Console.WriteLine("To:             {0}", obj.To);
            Console.WriteLine("From:           {0}", obj.From);
            Console.WriteLine("ResultMessage:  {0}", obj.ResultMessage);

            Console.ReadLine();
        }
    }

    [Serializable]
    [XmlRoot("data")]
    public class SMSSendingResponse
    {
        public SMSSendingResponse() {}

        //should come from the "status" xml element
        [XmlElement("status")]
        public string Status { get; set; }

        //should come from the "message" xml element (in our example - "OK")
        [XmlElement("message")]
        public string Message { get; set; }

        //should come from the results/result/account element. in our example "12345"
        [XmlIgnore()]
        public string AccountNumber
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.AccountNumber : null;
            }
        }

        //should come from the "id" xml element (in our example - "f8d3eea1cbf6771a4bb02af3fb15253e")
        [XmlIgnore()]
        public string ResponseID
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.ResponseID : null; 
            }
        }

        [XmlIgnore()]
        public string To
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.To : null;
            }
        }

        [XmlIgnore()]
        public string From
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.From : null;
            }
        }

        [XmlIgnore()]
        public string ResultMessage
        {
            get
            {
                Result r = FirstResult;
                return (r != null) ? r.Message : null;
            }
        }

        [XmlArray("results"), XmlArrayItem("result", typeof(Result))]
        public List<Result> Results
        {
            get { return (_Results); }
            set { _Results = value; }
        } private List<Result> _Results = new List<Result>();

        [XmlIgnore()]
        public Result FirstResult
        {
            get
            {
                return (_Results != null && _Results.Count > 0) ? _Results[0] : null;
            }
        }

    }

    [XmlType(TypeName = "result"), Serializable]
    public class Result
    {
        public Result() {}

        [XmlElement("account")]
        public string AccountNumber { get; set; }

        [XmlElement("to")]
        public string To { get; set; }

        [XmlElement("from")]
        public string From { get; set; }

        [XmlElement("message")]
        public string Message { get; set; }

        [XmlElement("id")]
        public string ResponseID { get; set; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文