如何在Xml中转换Nexted Model

时间:2015-09-17 06:17:20

标签: xml c#-4.0 xml-parsing

我想在XML文件中解析我的模型,我不知道如何解析这个...

看我的模特是: -

public partial class tblparent
    {
        public tblparent()
        {
            this.tblChild = new HashSet<tblChild>();
        }

        public int Id { get; set; }
        public string Name{ get; set; }
        public System.DateTime dtApplicationDate { get; set; }

        public virtual ICollection<tblChild > tblChild{ get; set; }
    }

public partial class tblparent
{
            public int Id { get; set; }
            public string Name{ get; set; }
            public int foregingkey {get; set;}  <-- Foregin key with Id in parent table
}

现在我想生成像这样的XML文件.....

<DocumentElement>
  <XMLDate>   <--- Parent Table
    <Id>0</Id>
    <Name>fdg</Name>
    <dtApplicationDate >1/1/2001</dtApplicationDate >
    <XMLCHild>  <-- Child Table
             <Id>0</Id>
             <Name>fdg</Name>
             <foregingkey>0</foregingkey>
    </XMLCHild>
  </XMLDate>
</DocumentElement>

请给我这个想法......

此致 Vinit Patel

1 个答案:

答案 0 :(得分:1)

试试这个

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication48
{
    class Program
    {
        static void Main(string[] args)
        {
            Tblparent tblparent = new Tblparent()
            {
                Id = 0,
                Name = "fdg",
                dtApplicationDate = DateTime.Parse("1/1/2001"),
                tblChild = new List<TblChild>() {
                    new TblChild() { Id = 0, Name = "fdg", foregingkey = 0}
                }
            };

            XElement[] children = tblparent.tblChild.Select(x => new XElement("XMLCHild",
                        new XElement[] {
                            new XElement("Id", x.Id),
                            new XElement("Name", x.Name),
                            new XElement("foregingkey", x.foregingkey)
                        })).ToArray();


            XElement element = new XElement("DocumentElement",
                new XElement("XMLDate", new object[] {
                    new XElement("Id",tblparent.Id),
                    new XElement("Name", tblparent.Name),
                    new XElement("dtApplicationDate", tblparent.dtApplicationDate),
                    tblparent.tblChild.Select(x => new XElement("XMLCHild",
                        new XElement[] {
                            new XElement("Id", x.Id),
                            new XElement("Name", x.Name),
                            new XElement("foregingkey", x.foregingkey)
                        }
                    )).ToArray()
                }));

        }


    }
    public partial class Tblparent
    {
        public Tblparent()
        {
            //this.tblChild = new HashSet<tblChild>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public System.DateTime dtApplicationDate { get; set; }

        public virtual ICollection<TblChild> tblChild { get; set; }
    }

    public partial class TblChild
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int foregingkey { get; set; }
    }
}
​
&#13;
&#13;
&#13;