How to generate XML with attributes in c#.
Posted
by user292815
on Stack Overflow
See other posts from Stack Overflow
or by user292815
Published on 2010-03-17T00:56:42Z
Indexed on
2010/03/17
1:01 UTC
Read the original article
Hit count: 557
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.
© Stack Overflow or respective owner