0
我有這樣的代碼:如何序列化爲包含屬性的XML?
...
request data = new request();
data.username = formNick;
xml = data.Serialize();
...
[System.Serializable]
public class request
{
public string username;
public string password;
static XmlSerializer serializer = new XmlSerializer(typeof(request));
public string Serialize()
{
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = Encoding.UTF8;
serializer.Serialize(
System.Xml.XmlWriter.Create(builder, settings),
this);
return builder.ToString();
}
public static request Deserialize(string serializedData)
{
return serializer.Deserialize(new StringReader(serializedData)) as request;
}
}
我想屬性添加到一些節點,並創建一些子節點。另外如何解析這樣的XML:
<answer>
<player id="2">
<coordinate axis="x"></coordinate>
<coordinate axis="y"></coordinate>
<coordinate axis="z"></coordinate>
<action name="nothing"></action>
</player>
<player id="3">
<coordinate axis="x"></coordinate>
<coordinate axis="y"></coordinate>
<coordinate axis="z"></coordinate>
<action name="boom">
<1>1</1>
<2>2</2>
</action>
</player>
</answer>
它不是一個XML文件,它是來自HTTP服務器的答案。
http://msdn.microsoft.com/en-us/library/182eeyhh.aspx。在問一個問題之前,請至少做一些研究。你會很快找到http://msdn.microsoft.com/en-us/library/2baksw0z.aspx,比等我告訴你更快。 – 2010-03-17 01:10:19
我做了研究,我如何得到我的代碼。最有趣的事情 - 很多人使用不同的方法來生成/解析XML ...這就是爲什麼我在這裏問它。 – Andoriyu 2010-03-18 19:22:43