views:

281

answers:

3

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.

A: 

See here Attributes That Control XML Serialization as well.

Preet Sangha
I tried but examples is so complicated.
Andoriyu
A: 

Try looking up some info on System.Xml

XmlTextReader xml = new XmlTextReader("http://example.com/XmlFile.xml");

If you aren't working with large amounts of data, you should use a dom parser. They parse the entire document into an array (or other data object), an example would be http://www.devx.com/xml/Article/10136 If you are going through a lot of data, I would use C# native classes, like: http://www.c-sharpcorner.com/UploadFile/sahuja/XmlParser11262005041621AM/XmlParser.aspx

Thanks for clearing up my confusion john ;)

Edit: Use something like

XmlDocument xml = new XmlDocument();
xml.LoadXml("XML STRING HERE"); //replace the string with the xml you got.

XmlNode node = xml.SelectSingleNode("XPATHCODEHERE"); //use the xpath syntax to find a the node you want to edit
XmlAttributeCollection nodeAttributes = node.Attributes; //Get the attributes for a node
XmlNode n = xml.CreateNode(XmlNodeType.Element, "XMLTagName", null); //Create a new xml tag
node.AppendChild(n); //append the new xml tag (n) inside XmlNode 'node'

Look at these resources:

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx

http://www.w3schools.com/xpath/xpath_syntax.asp

http://www.omegacoder.com/?p=46

mazzzzz
>XmlTextReader xml = new XmlTextReader("http://example.com/XmlFile.xml");You don't understand me, there is no xml files. Client (c#) sends xml request to server and server send xml answer. I already have that feature, but i don't understand how-to add tributes to some nodes and parse answer with attributes.
Andoriyu
@mazzzzz: -1. `new XmlTextReader()` is deprecated as of .NET 2.0. Use `XmlReader.Create()` instead.
John Saunders
AFAIK. unity3d uses mono, mono now cover only .net 2.0.
Andoriyu
@user: `XmlReader.Create` is in .NET 2.0, and most certainly is available in Mono.
John Saunders
A: 

It would be best if you had an XSD file describing the XML you will be receiving from the server. You could then use the XSD.EXE program to produce .NET classes with the appropriate .NET attributes on them. You could then just use XmlSerializer.Deserialize.

I'm going to try to create such a class for you by hand. This will be a quick attempt, and may be wrong (I have to get back to work!)


Try this and see if it works.

using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;


[XmlRoot("answer")]
public class Answer
{
    [XmlElement]
    public List<Player> Players { get; set; }
}

public class Player
{
    [XmlAttribute("id")]
    public int ID { get; set; }

    [XmlElement]
    public List<Coordinate> Coordinates { get; set; }

    [XmlElement("action")]
    public PlayerAction Action { get; set; }
}

public class PlayerAction
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlAnyElement]
    public XmlElement[] ActionContents { get; set; }
}

public enum Axis
{
    [XmlEnum("x")]
    X,
    [XmlEnum("y")]
    Y,
    [XmlEnum("z")]
    Z
}

public class Coordinate
{
    [XmlAttribute("axis")]
    public Axis Axis { get; set; }

    [XmlText]
    public double Value { get; set; }
}
John Saunders
and how to generate and parese xml with that code>
Andoriyu