LoginSignup
1
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-12-16

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

sample.cs
[XmlRoot("feed")]
public class MyType {...}


public static MyType FromXml(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(MyType ));
    return (MyType) serializer.Deserialize(new StringReader(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")]
public class MyType {...}

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

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0