NickMalloy
asked on
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;
}
The XDocument you are trying to create for srcTree doesn't have a root element.
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())
)
)
);
ASKER
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").E lements()
select el
)
);
doc = new XDocument(
new XComment("This is a comment"),
new XElement("Table",
from el in srcTree.Element("Table").E
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.