Reputation: 2435
What i'm trying to do is return XmlDocument
from my WCF Service.
The problem is that i got an error "Root element is missing."
here is my code
public XmlElement GetDeviceListXML()// this got [XmlSerializerFormat]and [OperationContract]
{
List<Device> list = MyProject.BLL.Device.GetList();// here i getting list of devices from my database
//Device is object which got [serializable] attribute
XmlRootAttribute xra = new XmlRootAttribute("Device");
xra.ElementName = "Devices";
xra.Namespace = "http://MMEwidencja.pl";
xra.IsNullable = false;
XmlSerializer serializer = new XmlSerializer(typeof(List<Device>), xra);
var stream = new MemoryStream();
XmlDocument xDoc = new XmlDocument();;
try
{
serializer.Serialize(stream, list);
stream.Position = 0;// that what was I miss
xDoc.Load(stream);
}
catch (Exception ex)
{
throw ex;
}
return xDoc.DocumentElement;
}
How should I make this work?
Edited: I've got solution for this the problem was that XmlDocument tried to Load stream from last byte to last byte. I've missed to put Position to 0 in that stream.
Upvotes: 0
Views: 488
Reputation: 31760
It would be much cleaner to return the xml as a stream of text and then to load it into a XmlDocument on the consumer.
Upvotes: 1
Reputation: 817
There's no root element because the XmlDocument.Load function thinks the stream has a 0 byte length.
Maybe read this link : http://geekswithblogs.net/.NETonMyMind/archive/2007/11/15/116862.aspx
Upvotes: 1