Tôi đang cố gắng deserialize một xml Atom được tạo ra bởi một trong các hệ thống nội bộ. Tuy nhiên, khi tôi cố gắng:Làm thế nào tôi có thể deserialize xml với một không gian tên mặc định?
public static MyType FromXml(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyType));
return (MyType) serializer.Deserialize(new StringReader(xml));
}
nó ném một ngoại lệ vào định nghĩa của không gian tên:
System.InvalidOperationException: <feed xmlns='http://www.w3.org/2005/Atom'> was not expected.
Khi tôi thêm không gian tên cho các nhà xây dựng của XmlSerializer, đối tượng của tôi là hoàn toàn trống rỗng:
public static MyType FromXml(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyType), "http://www.w3.org/2005/Atom");
return (MyType) serializer.Deserialize(new StringReader(xml)); //this will return an empty object
}
Bất kỳ ý tưởng nào tôi có thể làm cho nó hoạt động?
Chúc mừng, đã thực hiện công việc. – Grzenio