0

男孩,我在 Linq 上很密集吗?尤其是 Linq to XML。我正在努力扭转这个...

<?xml version="1.0"?>
<Template>
    <TemplateData>
        <Data>
            <ACTIVE>A</ACTIVE>
            <FIRST_NAME> JASON</FIRST_NAME>
            <LAST_NAME>ABATE</LAST_NAME>
            <DOB>2/11/2013 12:01:36 PM</DOB>
            <PHONE/>
                </Data>
         </TemplateData>
</Template>

进入这个...

IEnumerable<DatModel>

DatModel 等于这个...

public class DatModel{
        public int ACTIVE{ get; set; }
        public int FIRST_NAME{ get; set; }
        public int LAST_NAME{ get; set; }
        public int DOB{ get; set; }
        public string PHONE{ get; set; }
}

这就是我想要做的......

    IEnumerable<DatModel> templates = (IEnumerable<DatModel>)
                            from templatesList in xDocument.Descendants("Template")
                            select new TemplateModel{
    Template = (
                                from template in templatesList.Element("TemplateData").Elements("Data")
    select new DataModel{
    ACTIVE = template.Element("ACTIVE").Value,
)}.ToList()
};

我的清单一直是空的。我不确定为什么会这样。我究竟做错了什么?

4

2 回答 2

1

稍微清理一下您的代码以使其编译并添加一些 ToList(),它似乎可以工作。(我使用匿名类)

private static String s = @"<?xml version=""1.0""?>
    <Template>
    <TemplateData>
    <Data>
        <ACTIVE>A</ACTIVE>
        <FIRST_NAME> JASON</FIRST_NAME>
        <LAST_NAME>ABATE</LAST_NAME>
        <DOB>2/11/2013 12:01:36 PM</DOB>
        <PHONE/>
            </Data>
        </TemplateData>
    </Template>";

[Test]
public void myTest()
{

    XDocument xDocument = XDocument.Parse(s);

    var t = (from templatesList in xDocument.Descendants("Template")
            select new
                {
                    Template = (from template in templatesList.Element("TemplateData").Elements("Data")
                                select new
                                    {
                                        ACTIVE = template.Element("ACTIVE").Value
                                    }).ToList()
                }).ToList();

    Assert.AreEqual("A",t[0].Template[0].ACTIVE);

}

于 2013-02-14T17:11:39.560 回答
0

您的模型定义不正确(数据类型错误)。还要使用正确的命名对话。

尝试这个:

public class DatModel
{
    public string Active { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Dob { get; set; }
    public string Phone { get; set; }
}


string xml = @"<?xml version=""1.0""?>
                <Template>
                    <TemplateData>
                        <Data>
                            <ACTIVE>A</ACTIVE>
                            <FIRST_NAME> JASON</FIRST_NAME>
                            <LAST_NAME>ABATE</LAST_NAME>
                            <DOB>2/11/2013 12:01:36 PM</DOB>
                            <PHONE/>
                                </Data>
                            </TemplateData>
                </Template>";

XDocument doc = XDocument.Parse(xml);
IEnumerable<DatModel> datModels =
    doc.Descendants("Data").Select(d => new DatModel
    {
        Active = (string) d.Element("ACTIVE"),
        FirstName = (string) d.Element("FIRST_NAME"),
        LastName = (string)d.Element("LAST_NAME"),
        Dob = (DateTime) d.Element("DOB"),
        Phone = (string)d.Element("PHONE"),
    });
于 2013-02-14T16:56:35.010 回答