Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9301

デフォルトの名前空間でxmlをデシリアル化するには?

$
0
0

下記のコードで次のようなエラーが出てしまう。

sample.cs
[XmlRoot("feed")]publicclassMyType{...}publicstaticMyTypeFromXml(stringxml){XmlSerializerserializer=newXmlSerializer(typeof(MyType));return(MyType)serializer.Deserialize(newStringReader(xml));}
error
System.InvalidOperationException: <feed xmlns='http://www.w3.org/2005/Atom'> was not expected.

属性に名前空間を指定することで解決できる。

result.cs
[XmlRoot("feed",Namespace="http://www.w3.org/2005/Atom")]publicclassMyType{...}

参照:stackoverflow
https://stackoverflow.com/questions/1232311/how-can-i-deserialize-xml-with-a-default-namespace


Viewing all articles
Browse latest Browse all 9301

Trending Articles