将xml反序列化为类。我想知道我的班级结构应该是什么

时间:2015-09-21 04:24:46

标签: c# xml

我想将xml反序列化为一个类。请让我知道我将如何组成班级 我的xml: -



<?xml version="1.0" encoding="utf-8"?>
<cXML xml:lang="en-US"
	  timestamp="2002-08-15T08:36:47-07:00">
	<Header>
		<From>
			<Credential domain="ex:ThirdParty">
				<Identity>ex:65652314</Identity>
			</Credential>
		</From>
		<To>
			<Credential domain="ex:xyz">
				<Identity>ex:Punchout</Identity>
			</Credential>
		</To>
		<Sender>
			<Credential domain=" ex:ThirdParty.com ">
				<Identity>ex: Third Party website Credential</Identity>
				<SharedSecret>ex: Third Party Password</SharedSecret>
			</Credential>
			<UserAgent>ex:Third Party Procurement System 2.0</UserAgent>
		</Sender>
	</Header>
	<Request>
		<PunchOutSetupRequest operation="create">
			<BuyerCookie>ex:34234234ADFSDF234234</BuyerCookie>
			<BrowserFormPost>
				<URL>ex:https://thirdpart.com/punchoutexit</URL>
			</BrowserFormPost>
		</PunchOutSetupRequest>
	</Request>
</cXML>
&#13;
&#13;
&#13; 我必须设计一个类来反序列化这个xml

1 个答案:

答案 0 :(得分:0)

如何做到这一点已经讨论here

这就是你的班级的样子。

/// <remarks />
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class cXML
{
    /// <remarks />
    public cXMLHeader Header { get; set; }

    /// <remarks />
    public cXMLRequest Request { get; set; }

    /// <remarks />
    [XmlAttribute(Form = XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
    public string lang { get; set; }

    /// <remarks />
    [XmlAttribute]
    public DateTime timestamp { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLHeader
{
    /// <remarks />
    public cXMLHeaderFrom From { get; set; }

    /// <remarks />
    public cXMLHeaderTO To { get; set; }

    /// <remarks />
    public cXMLHeaderSender Sender { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLHeaderFrom
{
    /// <remarks />
    public cXMLHeaderFromCredential Credential { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLHeaderFromCredential
{
    /// <remarks />
    public string Identity { get; set; }

    /// <remarks />
    [XmlAttribute]
    public string domain { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLHeaderTO
{
    /// <remarks />
    public cXMLHeaderTOCredential Credential { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLHeaderTOCredential
{
    /// <remarks />
    public string Identity { get; set; }

    /// <remarks />
    [XmlAttribute]
    public string domain { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLHeaderSender
{
    /// <remarks />
    public cXMLHeaderSenderCredential Credential { get; set; }

    /// <remarks />
    public string UserAgent { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLHeaderSenderCredential
{
    /// <remarks />
    public string Identity { get; set; }

    /// <remarks />
    public string SharedSecret { get; set; }

    /// <remarks />
    [XmlAttribute]
    public string domain { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLRequest
{
    /// <remarks />
    public cXMLRequestPunchOutSetupRequest PunchOutSetupRequest { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLRequestPunchOutSetupRequest
{
    /// <remarks />
    public string BuyerCookie { get; set; }

    /// <remarks />
    public cXMLRequestPunchOutSetupRequestBrowserFormPost BrowserFormPost { get; set; }

    /// <remarks />
    [XmlAttribute]
    public string operation { get; set; }
}

/// <remarks />
[XmlType(AnonymousType = true)]
public class cXMLRequestPunchOutSetupRequestBrowserFormPost
{
    /// <remarks />
    public string URL { get; set; }
}