ASP.NET MVC 2:反序列化 SPROC 响应

发布于 2024-10-10 06:10:44 字数 401 浏览 2 评论 0原文

我需要问一个一般性问题。我面前没有代码,因为我是在 iPhone 上编写的。

我有一个代表特定 XML 模式的类。我有一个返回此 XML 的 SPROC。我需要做的是将 XML 反序列化到此类。

XML:

<xml>
     <person>
             <firstName>Bob</firstName>
             <lastName>Robby</lastName>
     </person>
</xml>

我需要将此 XML 反序列化到自定义 Person 类中,以便我可以循环此模型并将其吐出到视图中。我确信涉及某种选角,我只是不知道该怎么做。

I need to ask a general question. I don't have the code in front of me because I'm writing this on my iPhone.

I have a Class that represents a certain XML schema. I have a SPROC that returns this XML. What I need to do is deserialize the XML to this Class.

XML:

<xml>
     <person>
             <firstName>Bob</firstName>
             <lastName>Robby</lastName>
     </person>
</xml>

I need to deserialize this XML into the custom Person Class so I can loop through this Model and spit it out in the View. I'm sure there's some kind of casting involved, I just don't know how to do it.

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

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

发布评论

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

评论(2

贱人配狗天长地久 2024-10-17 06:10:44

我的解决方案:

 public class Program {
        public static void Main(string[] args) {


            string xml = @"<xml><person><firstName>Bob</firstName><lastName>Robby</lastName></person></xml>";

            var doc = XElement.Parse(xml);
            var person = (from x in doc.Elements("person") select x).FirstOrDefault();

            XmlSerializer serializer = new XmlSerializer(typeof(Person));

            var sr = new StringReader(person.ToString());
            // Use the Deserialize method to restore the object's state.
            var myPerson = (Person)serializer.Deserialize(sr);

        }

    }

和班级:

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

namespace ConsoleApplication3 {

    [XmlRoot("person")]
    public class Person {

        [XmlElement("firstName")]
        public string FirstName { get; set; }

        [XmlElement("lastName")]
        public string LastName { get; set; }
    }

}

My Solution:

 public class Program {
        public static void Main(string[] args) {


            string xml = @"<xml><person><firstName>Bob</firstName><lastName>Robby</lastName></person></xml>";

            var doc = XElement.Parse(xml);
            var person = (from x in doc.Elements("person") select x).FirstOrDefault();

            XmlSerializer serializer = new XmlSerializer(typeof(Person));

            var sr = new StringReader(person.ToString());
            // Use the Deserialize method to restore the object's state.
            var myPerson = (Person)serializer.Deserialize(sr);

        }

    }

And Class:

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

namespace ConsoleApplication3 {

    [XmlRoot("person")]
    public class Person {

        [XmlElement("firstName")]
        public string FirstName { get; set; }

        [XmlElement("lastName")]
        public string LastName { get; set; }
    }

}
像你 2024-10-17 06:10:44

在 linq 中它会是这样的

XDocument xmlFile = XDocument.Parse(yourXml)    
var people = (from x in xmlFile.Descendants("person")
              select new Person(){
                      firstname = (string)x.Element("firstname").Value,
                      lastname = (string)x.Element("lastname").Value
              });

in linq it would be something like this

XDocument xmlFile = XDocument.Parse(yourXml)    
var people = (from x in xmlFile.Descendants("person")
              select new Person(){
                      firstname = (string)x.Element("firstname").Value,
                      lastname = (string)x.Element("lastname").Value
              });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文