assignment to xml node value

public class AddAnnouncement

{

public void _AddAnnouncement()

{

XmlDocument newAnnDoc = new XmlDocument();

newAnnDoc.Load("~/App_Data/Announcement.xml");

XmlNode newAnn = newAnnDoc.CreateNode(XmlNodeType.Element, "ann", null);

 

XmlNode Id = newAnnDoc.CreateNode(XmlNodeType.Element, "annID", null);

 

/////////////////////in here i want to assign to id value to 0; after that i want to increase it;

like this int a=0; and id. ( maybe value;;; i dont know what mus be after dot)=a; a++;

how can i do this

XmlNode Title = newAnnDoc.CreateNode(XmlNodeType.Element, "annTitle", null);

XmlNode Body = newAnnDoc.CreateNode(XmlNodeType.Element, "annBody", null);

XmlNode Date = newAnnDoc.CreateNode(XmlNodeType.Element, "annDate", null);

XmlNode Priority = newAnnDoc.CreateNode(XmlNodeType.Element, "annPriority", null);

XmlNode Sender = newAnnDoc.CreateNode(XmlNodeType.Element, "annSender", null);

XmlElement annPanel = newAnnDoc.DocumentElement;

annPanel.AppendChild(newAnn);

newAnn.AppendChild(Id);

newAnn.AppendChild(Title);

newAnn.AppendChild(Body);

newAnn.AppendChild(Date);

newAnn.AppendChild(Priority);

newAnn.AppendChild(Sender);

}

}




Answer this question

assignment to xml node value

  • Jorne

    thanks too much for your help;;

  • Chris Lively

    The InnerText property of an element will be the inner text so you should be able to do this:

    Id.InnerText= (a++).ToString();

    This takes the current value of a, converts it to a string, then increments a and finally assigns the string value to the node. Of course if you are incrementing a through some other means then simply call ToString().

    Michael Taylor - 8/2/06


  • Tone Southerland

    i need only return value in my method ;

    could i write like this

    return nodeLast.InnerText = (++id).ToString(); ////is this true definition

    because i will use in my addannouncement method like this

    XmlNode Id = newAnnDoc.CreateNode(XmlNodeType.Element, "annID", null);

    Id.Value = GetID();



  • DrJazz

    If you are guaranteed that the announcements will be sequentially incrementing then you can use an XPath query to retrieve the last announcement. Then get its InnerText value and convert it to an int. Increment the value and set the new announcement's InnerText to the result.

    If there are no announcements yet then the XPath query will return nothing so you should set the initial ID to 0 or whatever. If you can guarantee that all code goes through this same block then you technically only have to retrieve the ID once. After that you can store it in a member field and reuse it (assuming you can keep the instance lying around).

    If you are not guaranteed that the announcements are in order then you should use an XPath query to retrieve all the announcements, iterate through each one and save off the highest ID. Then increment the value and assign like earlier.

    static void Main ( string[] args )
    {
    //Load the document
    XmlDocument doc = new XmlDocument();
    doc.Load(...
    );

    int id = 0;

    //Find the last announcement and id
    XmlNode nodeLast = doc.SelectSingleNode("//anns/ann[position()=last()]");
    if (nodeLast != null)
    id =
    Convert.ToInt32(nodeLast.InnerText);

    //Create the new announcement
    XmlNode nodeNew = doc.CreateElement("ann");
    XmlNode nodeId = doc.CreateElement("annID");
    nodeId.InnerText = (++id).ToString();
    nodeNew.AppendChild(nodeId);

    //Save
    doc.DocumentElement.AppendChild(nodeNew);
    doc.Save(
    ...);
    }

    Michael Taylor - 8/2/06


  • todd_bulky

    ok thanks;

    how can i reach annpanel(root).lastchild.id.value from other method ; because i must get last child id no value and after increase it

    like this

    public int increaseid/// how must i write this code

    {

    a=0;

    annpanel.lastchild.id(this is my is node).value = a;/// i cant reach lastchild id value from addannouncement method

    return ++a;

    }

    how can i do this

    so after that i will do this

    id.value = increaseid();

    can you help me about this problem;



  • charret

    The syntax you are using is wrong. If you want to return the last value used then do this:

    return Convert.ToInt32(nodeLast.InnerText);

    If you want the next value to use then do this:

    return ++Convert.ToInt32(nodeLast.InnerText);

    Michael Taylor - 8/2/06


  • assignment to xml node value