0

私はこのXML要素文字列を持っています:

<person name="jhon smith" birth="11/10/1988" username="ilearn" password="123"/>

ここで、これをそれぞれのオブジェクトに逆シリアル化します。

public class CancelCardResponse
{
    public string name { get; set; }
    public string birth { get; set; }
    public string username { get; set; }
    public string password { get; set; }
}

私はこれに似たコードを使用しています:

XmlSerializer deserializer = new XmlSerializer(typeof(Person));
StringReader reader = new StringReader(myxmlelementstring);

var a = deserializer.Deserialize(reader); // fail!

エラーは次のようになります。

System.InvalidOperationException {" XMLドキュメント(1,2)にエラーがあります。 "}

上記のようなXML要素文字列を逆シリアル化することは可能ですか?
C#デシリアライザーを使用するためにXML要素文字列に追加できるものはありますか?

4

2 に答える 2

0

このようにしてみてください:

public class person
{
    [XmlAttribute]
    public string name { get; set; }

    [XmlAttribute]
    public string birth { get; set; }

    [XmlAttribute]
    public string username { get; set; }

    [XmlAttribute]
    public string password { get; set; }
}

私はそれをコンパイルしたりテストしたりしませんでした。ユーザーになってから少し時間が経ちましXmlSerializerたが、これでそこに到達するか、それに近づくはずです。

于 2012-12-06T20:50:42.973 に答える
0

CancelCardResponseクラスの名前をに変更しPerson、そのプロパティに注釈を付けます[XmlAttribute("Attr Name as in XML")]

于 2012-12-06T20:52:58.493 に答える