使用linq将xml解析为类

时间:2012-06-25 09:11:08

标签: .net linq

我有一个定义请求xml的类 我想使用linq将xml解析为类(稍后会执行相反的操作) 我得到的req为null并且在返回类的函数中所有的属性“MidaClientID名称'MidaClientID'在当前上下文中不存在”

public class Request
    {
        public int MidaClientID { get; set; }

        public int Password { get; set; }

        public int CandNum { get; set; }

        public string CandFirstName { get; set; }

        public string CandLastName { get; set; }

        public int CandPhone1 { get; set; }

        public int CandPhone2 { get; set; }

        public int CandPhone3 { get; set; }

        public string CandAttach1 { get; set; }

        public string CandAttach2 { get; set; }

        public string CandAttach3 { get; set; }

        public int ClientNum { get; set; }

        public string ClientName { get; set; }

        public int ContactNum { get; set; }

        public string ContactFirstName { get; set; }

        public string ContactLastName { get; set; }

        public string ContactEmail { get; set; }

        public int EventNum { get; set; }

        public DateTime EventDate { get; set; }

        public DateTime EventTime { get; set; }

        public int QuestNum { get; set; }

    }


 [WebMethod]
        public void GetXml()
        {
            XDocument doc = XDocument.Load(@".xml");
            List<Request> req = CreateRequest(doc);
        }


        public static List<Request> CreateRequest(XDocument input)
        {
            IEnumerable<Request> req = from r in input.Descendants("Request")
                                       select new Request()
                                       {
                                           //MidaClientID = (int)r.Attribute("MidaClientID"),
                                           MidaClientID = Convert.ToInt32(r.Element("MidaClientID").Value),
                                           Password = (int)r.Attribute("MidaClientPassword"),
                                           CandNum = (int)r.Attribute("CandNum"),
                                           CandFirstName = (string)r.Attribute("CandFirstName"),
                                           CandLastName = (string)r.Attribute("CandLastName"),
                                           CandPhone1 = (int)r.Attribute("CandPhone1"),
                                           CandPhone2 = (int)r.Attribute("CandPhone2"),
                                           CandPhone3 = (int)r.Attribute("CandPhone3"),
                                           CandAttach1 = (string)r.Attribute("CandAttach1"),
                                           CandAttach2 = (string)r.Attribute("CandAttach2"),
                                           CandAttach3 = (string)r.Attribute("CandAttach3"),
                                           ClientNum = (int)r.Attribute("ClientNum"),
                                           ClientName = (string)r.Attribute("ClientName"),
                                           ContactNum = (int)r.Attribute("ContactNum"),
                                           ContactFirstName = (string)r.Attribute("ContactFirstName"),
                                           ContactLastName = (string)r.Attribute("ContactLastName"),
                                           ContactEmail = (string)r.Attribute("ContactEmail"),
                                           EventNum = (int)r.Attribute("EventNum"),
                                           EventDate = (DateTime)r.Attribute("EventDate"),
                                           EventTime = (DateTime)r.Attribute("EventTime"),
                                           QuestNum = (int)r.Attribute("QuestNum")
                                       };
            return new List<Request> { };
        }

2 个答案:

答案 0 :(得分:0)

您可以通过序列化对象来完成此操作。

“序列化是获取对象状态并以某种方式持久化的过程.Microsoft .NET Framework包含可以将任何对象序列化为XML的强大对象.System.Xml.Serialization命名空间提供此功能。”

http://support.microsoft.com/kb/815813

答案 1 :(得分:0)

您获得null的原因是因为您要返回列表的新实例。

return new List<Request> { };

可以将上面的代码行替换为

return req;

如下面的代码所示。

public static List<Request> CreateRequest(XDocument input)
        {
            IEnumerable<Request> req = from r in input.Descendants("Request")
                                       select new Request()
                                       {
                                           //MidaClientID = (int)r.Attribute("MidaClientID"),
                                           MidaClientID = Convert.ToInt32(r.Element("MidaClientID").Value),
                                           Password = (int)r.Attribute("MidaClientPassword"),
                                           CandNum = (int)r.Attribute("CandNum"),
                                           CandFirstName = (string)r.Attribute("CandFirstName"),
                                           CandLastName = (string)r.Attribute("CandLastName"),
                                           CandPhone1 = (int)r.Attribute("CandPhone1"),
                                           CandPhone2 = (int)r.Attribute("CandPhone2"),
                                           CandPhone3 = (int)r.Attribute("CandPhone3"),
                                           CandAttach1 = (string)r.Attribute("CandAttach1"),
                                           CandAttach2 = (string)r.Attribute("CandAttach2"),
                                           CandAttach3 = (string)r.Attribute("CandAttach3"),
                                           ClientNum = (int)r.Attribute("ClientNum"),
                                           ClientName = (string)r.Attribute("ClientName"),
                                           ContactNum = (int)r.Attribute("ContactNum"),
                                           ContactFirstName = (string)r.Attribute("ContactFirstName"),
                                           ContactLastName = (string)r.Attribute("ContactLastName"),
                                           ContactEmail = (string)r.Attribute("ContactEmail"),
                                           EventNum = (int)r.Attribute("EventNum"),
                                           EventDate = (DateTime)r.Attribute("EventDate"),
                                           EventTime = (DateTime)r.Attribute("EventTime"),
                                           QuestNum = (int)r.Attribute("QuestNum")
                                       };
            return req;

}