views:

30

answers:

1

how can I add to each node I add to xml a unique index value for example I have

    <events>
        <event><id>0</id>
</event>
        <event><id>1</id></event>
    </events>

I want to add another event "node" that the id will be 6

/edited/

i have created xsd an added to the id elment autoincrease =true but idont really know what to do next ? help

+1  A: 

The easiest way to serialize an attribute with an object is to decorate the property with [XmlAttribute]. Here is a simple example of a class representing your <event> element:

[XmlType(TypeName="event")]
public class Event
{
    [XmlAttribute("id")]
    public int ID { get; set; }
}

You might get some mileage out of the Xsd.exe command line tool. In some cases, the tool can save you a lot of hand-decorating classes for serialization. It can be used in a two-step fashion to:

  1. Generate a XSD validation schema for a sample XML file.
  2. Generate serializable .NET classes based on the XSD schema.
kbrimington
i already done tha, but i need the id to automatically know it suppose to be 7
maggie
@maggie: The serializer will not do that for you. You must assign a value to ID prior to serialization. Since you must already have your events in some collection, it should be trivial to loop through them and assign IDs.
kbrimington
arrrr :-) that is excatly my question// how to assign this value..how would i know what number to assign ?
maggie
@maggie: you need to keep track of the highest value used so far yourself, in your code, and then just add the next higher value. There's nothing in XML or XML Schema to do this automagically for you...
marc_s
thanks , i thought there was other way
maggie