I have a .Net webserivce that accepts XML
in string format. XML
String sent into the webserivce can represent any Object in the system. I need to check
If you have an XmlDocument
, you can use XmlNodeReader
as an XmlReader
to pass to XmlSerializer
, but I wonder if it would be better to do it the other way; use an XmlReader
to get the outermost element name, and give that to XmlSerializer
...
[XmlRoot("foo")]
public class Foo
{
[XmlAttribute("id")]
public int Id { get; set; }
}
static class Program
{
static void Main()
{
string xml = " ";
object obj;
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
reader.MoveToContent();
switch (reader.Name)
{
case "foo":
obj = new XmlSerializer(typeof(Foo)).Deserialize(reader);
break;
default:
throw new NotSupportedException("Unexpected: " + reader.Name);
}
}
}
}