2

我有一个定义请求 xml 的类我想使用 linq 将 xml 解析为类(稍后做相反的事情)我得到 req 为空,并且在返回类所有属性的函数中“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> { };
        }
4

2 回答 2

0

您可以通过序列化对象来做到这一点。

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

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

于 2012-06-25T09:16:04.703 回答
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;

}

于 2012-06-26T04:38:40.577 回答