Linq to XML

I'm trying to take a linq query and convert it over to XML using XDocument. My problem is that I am getting an error. "This operation would create an incorrectly structured document"  What am I missing?
public XDocument CreateXml(string id)
    {
        // int rec = Convert.ToInt32(id);
        int rec = Convert.ToInt32(5340);
        XDocument doc = new XDocument();
        using (var ctx = new CarAppsEntities())
        {
            var p3 = ctx.tblCarRequests.SingleOrDefault(u => u.CarID == rec);
            if (p3 != null)
            {

                DateTime theStartdate = Convert.ToDateTime(p3.StartDate);
                DateTime theEnddate = Convert.ToDateTime(p3.EndDate);
                XDocument srcTree = new XDocument(
                new XComment("This is a comment"),
                new XElement("NewDataSet"),
                new XElement("Table",
                    new XElement("CarID", p3.CarID),
                    new XElement("CarOwner", p3.CarOwner),
                    new XElement("Description", p3.Description),
                    new XElement("StartDate", theStartdate.ToShortDateString()),
                    new XElement("EndDate", theEnddate.ToShortDateString())

                )
            );

                doc = new XDocument(
                new XComment("This is a comment"),
                new XElement("Table",
                    from el in srcTree.Element("Table").Elements()
                    select el
                )
            );
            }
        }
        return doc;
    }

Open in new window

NickMalloyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
wdosanjosConnect With a Mentor Commented:
Please try:

 doc = new XDocument(
                new XComment("This is a comment"),
                new XElement("Table",
                    from el in srcTree.Element("NewDataSet").Element("Table").Elements()
                    select el
                )
            );

Open in new window

0
 
effesCommented:
The XDocument you are trying to create for srcTree doesn't have a root element.
0
 
wdosanjosCommented:
Try changing your srcTree instantiation statement as follows.  You were trying to create multiple root elements, which is not permitted.

XDocument srcTree = new XDocument(
				new XComment("This is a comment"),
				new XElement("NewDataSet",
					new XElement("Table",
						new XElement("CarID", p3.CarID),
						new XElement("CarOwner", p3.CarOwner),
						new XElement("Description", p3.Description),
						new XElement("StartDate", theStartdate.ToShortDateString()),
						new XElement("EndDate", theEnddate.ToShortDateString())
					)
				)
			);

Open in new window

0
 
NickMalloyAuthor Commented:
I changed it and now I am getting an "Object reference not set to an instance of an object." on this part

 doc = new XDocument(
                new XComment("This is a comment"),
                new XElement("Table",
                    from el in srcTree.Element("Table").Elements()
                    select el
                )
            );
public XDocument CreateXml(string id)
    {
        // int rec = Convert.ToInt32(id);
        int rec = Convert.ToInt32(5340);
        XDocument doc = new XDocument();
        using (var ctx = new CarAppsEntities())
        {
            var p3 = ctx.tblCarRequests.SingleOrDefault(u => u.CarID == rec);
            if (p3 != null)
            {

                DateTime theStartdate = Convert.ToDateTime(p3.StartDate);
                DateTime theEnddate = Convert.ToDateTime(p3.EndDate);
                XDocument srcTree = new XDocument(
                    new XComment("This is a comment"),
                    new XElement("NewDataSet",
                        new XElement("Table",
                        new XElement("CarID", p3.CarID),
                        new XElement("CarOwner", p3.CarOwner),
                        new XElement("Description", p3.Description),
                        new XElement("StartDate", theStartdate.ToShortDateString()),
                        new XElement("EndDate", theEnddate.ToShortDateString())
                        )
                        )
                        );

                doc = new XDocument(
                new XComment("This is a comment"),
                new XElement("Table",
                    from el in srcTree.Element("Table").Elements()
                    select el
                )
            );
            }
        }
        return doc;
    }

Open in new window

0
All Courses

From novice to tech pro — start learning today.