setting XML attribute

I'm trying to set a XML attribute whenever someone presses a radio button for example:

before radio button click
<row type="">
after radiobutton click
<row type="header">

i can select the row and change the value however i am having problems writing the change back to the file.

if i use the xmltextwriter it seems to clear the file

the code im using to get to the button currently is
XmlNodeList nodelst = _doc.GetElementsByTagName("row");
foreach (XmlNode node in nodelst)
{
XmlAttribute attr = node.Attributes["type"];
attr.Value = "header";
}

can anyone help at all

Colin Gemmell


Answer this question

setting XML attribute

  • canadian_coder

    This XmlTextWriter is not linked to your document, replace the above block of code with this:

    try
    {
    XmlNodeList nodelst = _doc.GetElementsByTagName("row");
    // XmlReader rr = nodelst;
    foreach (XmlNode node in nodelst)
    {
    XmlAttribute attr = node.Attributes["type"];
    attr.Value = "header";
    }
    }
    XmlTextWriter tr = new XmlTextWriter("tabledata.xml", null);
    tr.Formatting = Formatting.Indented;
    _doc.WriteContentTo(tr);

    tr.Close();



  • Here2Play

    i've all ready writen so much to a file using a seperate xmltextwriter. but whenever i call the new one the file is completely empted of all tags. do you have to copy what is in the file allready somewhere then re-write the whole file


  • ClemsonTigers71

    cool, i've created an xml document and set it up with this code

    _doc.Load("tabledata.xml");
    //intialize from tag
    XmlElement newform = _doc.CreateElement(formname);
    newform.SetAttribute("norows", tb_norows.Text);
    .......

    //initalize row tag
    for (int r = 0; r < norows; r++)
    {
    XmlElement newrow = _doc.CreateElement("row");
    newrow.SetAttribute("type", "");
    ......
    for (int c = 0; c < nocolumns; c++)
    {
    XmlElement newcolumn = _doc.CreateElement("column");
    newcolumn.SetAttribute("no", Convert.ToString(c));
    .......
    newrow.AppendChild(newcolumn);
    }
    newform.AppendChild(newrow);
    }
    add row tag to form
    _doc.DocumentElement.AppendChild(newform);

    XmlTextWriter tr = new XmlTextWriter("tabledata.xml", null);
    tr.Formatting = Formatting.Indented;
    _doc.WriteContentTo(tr);
    tr.Close();

    so what i want to do now is change one of the element attributes to a different value with the code before.
    i can get the file created and initally filled. then on the event i can select the right node and attribute with this code:-
    XmlNodeList nodelst = _doc.GetElementsByTagName("row");
    // XmlReader rr = nodelst;
    foreach (XmlNode node in nodelst)
    {
    XmlAttribute attr = node.Attributes["type"];
    attr.Value = "header";
    tr.Flush();
    }
    however whenever i use the xmltextwriter it clears what is allready there in the file.
    that is all the code i have in the project that has anything to do with xml

    by the by thanks for the help



  • jhikel

    The flush method writes the file physicaly from memory to the drive.

  • Tariq Mehmood

    Place the tr.Flush() after the _doc.WriteContentTo(tr)

  • FinnErik

    XmlTextWriter tr = new XmlTextWriter("tabledata.xml",null);
    try
    {
    XmlNodeList nodelst = _doc.GetElementsByTagName("row");
    // XmlReader rr = nodelst;
    foreach (XmlNode node in nodelst)
    {
    XmlAttribute attr = node.Attributes["type"];
    attr.Value = "header";
    attr.WriteTo(tr.);
    tr.Flush();
    }
    }

    sorry missed that bit out by mistake

  • Mrzev

    How did you use the XmlTextWriter Maybe you forgot to flush the file.

  • Peter Wellington

    What other TextWriter I don't see it in your code. Did you fill it with the right XmlDoc

  • mistry_bhavin

    that worked. thank you so much for you time and patiants.

    thank you, thank you thank you.

  • Mair&amp;#233;ad - MSFT

    i created a new xmltextwriter in the procedure. however didnt flush it.

    sorry im quite new to using xml but would flushing the file do



  • Loadstone

    that part works fine and does create the output, adding the flush there has still makes the file empty when it hits the second xmltextwriter(which is in another procedure).

  • markgoldin

    Can you show me a bit more code, cause I'm not sure what you mean.

  • setting XML attribute