使用匿名数据类型编译XDocument

时间:2016-02-25 07:01:13

标签: xml linq anonymous-types

我有以下代码,它抛出以下错误:

"无效的匿名类型成员声明符。必须声明匿名类型成员...."

我的grp是一个列表> GRP

        var grp = lstCuts.GroupBy(x =>` x.Bar_id).Select(a=>a.ToList()).ToList();

        foreach(var bar in grp)
        {
            XElement bardata = new XElement("BAR",
                                    new XElement("BRAN", bar[0].Brand),
                                    new XElement("SYST", bar[0].System),
                                    new XElement("CODE", bar[0].Code),
                                    new XElement("DESC", bar[0].Description),
                                    new XElement("DICL", bar[0].DICL),
                                    new XElement("DOCL", bar[0].DOCL),
                                    new XElement("LEN", bar[0].Length),
                                    new XElement("STS", bar[0].BarStatus),
                                    new XElement("H", bar[0].H),
                                    new XElement("MLT", "1"),
                                    new XElement("CUT",
                                        from a in bar
                                        select XElement(new
                                        {
                                            new XElement("ANGL", a.AngleL.ToString()),
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                            new XElement("ANGL",)
                                        })

            //create a new bar and add all the cuts

我的预期输出类似于以下

    <BAR>
       <BRAN>ALSPEC</BRAN> 
       <SYST>McArthur 101.6mm Centre Pocket</SYST> 
       <CODE>AS1C</CODE> 
       <DESC>Main Frame - Captive</DESC> 
       <DICL>ANOGRP3</DICL> 
       <DOCL>25um Anodised</DOCL> 
       <LEN>6500</LEN> 
       <STS>1</STS> 
       <H>101.6</H> 
       <MLT>1</MLT> 
       <CUT>
        <NUM>1</NUM> 
        <TYPE /> 
        <ANGL>90</ANGL> 
        <ANGR>90</ANGR> 
        <AB1>90</AB1> 
        <AB2>90</AB2> 
        <IL>5816</IL> 
        <OL>5816</OL> 
        <BCOD>0000000475/1/4</BCOD> 
        <DESC>Jamb - Right</DESC> 
        <STAT>1</STAT> 
        <LBL>Job#878/Item#1</LBL> 
        <LBL>5816 mm</LBL> 
        <LBL>DW1</LBL> 
        <LBL>Jamb - Right</LBL> 
       </CUT>
<CUT>
        <NUM>1</NUM> 
        <TYPE /> 
        <ANGL>90</ANGL> 
        <ANGR>90</ANGR> 
        <AB1>90</AB1> 
        <AB2>90</AB2> 
        <IL>5816</IL> 
        <OL>5816</OL> 
        <BCOD>0000000475/1/4</BCOD> 
        <DESC>Jamb - Right</DESC> 
        <STAT>1</STAT> 
        <LBL>Job#878/Item#1</LBL> 
        <LBL>5816 mm</LBL> 
        <LBL>DW1</LBL> 
        <LBL>Jamb - Right</LBL> 
       </CUT>
<CUT>
        <NUM>1</NUM> 
        <TYPE /> 
        <ANGL>90</ANGL> 
        <ANGR>90</ANGR> 
        <AB1>90</AB1> 
        <AB2>90</AB2> 
        <IL>5816</IL> 
        <OL>5816</OL> 
        <BCOD>0000000475/1/4</BCOD> 
        <DESC>Jamb - Right</DESC> 
        <STAT>1</STAT> 
        <LBL>Job#878/Item#1</LBL> 
        <LBL>5816 mm</LBL> 
        <LBL>DW1</LBL> 
        <LBL>Jamb - Right</LBL> 
       </CUT>
      </BAR>

从我的谷歌搜索我知道我需要以某种方式命名这个类型,但我不确定这意味着什么。

1 个答案:

答案 0 :(得分:0)

使用与此类似的对象图 -

[XmlRoot(ElementName = "BAR")]
public class Bar
{
    public string BRAN { get; set; }
    public string SYST { get; set; }
    public string CODE { get; set; }
    public string DESC { get; set; }
    public string DICL { get; set; }
    public string DOCL { get; set; }
    public string LEN { get; set; }
    public string STS { get; set; }
    public string H { get; set; }
    public string MLT { get; set; }
    public List<Cut> Cuts { get; set; }

}

[XmlRoot(ElementName = "CUT")]
public class Cut
{
    public string NUM { get; set; }
    public string TYPE { get; set; }
    public string ANGL { get; set; }
    public string ANGR { get; set; }
    public string AB1 { get; set; }
    public string AB2 { get; set; }
    public string IL { get; set; }
    public string OL { get; set; }
    public string BCOD { get; set; }
    public string DESC { get; set; }
    public string STAT { get; set; }
    public LBL LBL { get; set; }
}

public class LBL
{
    [XmlArrayItem(ElementName = "LBL")]
    public List<string> Lbl { get; set; }
}

然后使用Bar序列化XmlSerializer对象。