Reputation: 28586
I have to export to XML a list of BusinessObjects - myBOs
I worked before with such a tasks, using XmlDocument.
Now, I read that System.Xml.Linq is more flexible to use. So I took XDocument.
Now the logic is the following (pseudo code):
Method ExportBOs(myBOs, fileName)
xDoc = New XDocument ' create
foreach bo in myBOs
xDoc.Add(GetMyBOAsNode(bo)) ' build
xDoc.Save(filename) ' save
Method GetMyBOAsNode(myBo) as XNode
result = New XNode(myBo.Name) ' ??? don't work
Return result
What is the way to deal with?
Upvotes: 1
Views: 1797
Reputation: 1993
First you need to have a single root element (multiple roots are not allowed).
Second, use System.Xml.Linq.XElement
:
public void ExportBOs(IEnumerable<myBO> myBOs, string fileName)
{
var root = new XElement("BOs");
foreach(var bo in myBOs){
root.Add(new XElement("BO", bo.Name));
}
root.Save(filePath);
}
Upvotes: 3
Reputation: 1504122
You have two problems:
XNode
is abstract. I suspect you actually want to create an XElement
XElement
, add it to the document, and then add multiple child elements to that root element.Upvotes: 5