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

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
ClemsonTigers71
_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
Tariq Mehmood
FinnErik
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
Peter Wellington
mistry_bhavin
thank you, thank you thank you.
Mair&#233;ad - MSFT
sorry im quite new to using xml but would flushing the file do
Loadstone
markgoldin