How to generate XML with attributes in .NET?
- by user292815
I have that code:
...
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;
}
}
I want to add attributes to some nodes and create some sub-nodes. Also how to parse xml like that:
<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>
p.s.
it is not a xml file, it's answer from http server.