Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is saving Multiple Button Locations in XML file.

C#
XmlDocument doc = new XmlDocument();
            doc.LoadXml("<button></button>");

            XmlElement newElem = doc.CreateElement("x");
            newElem.InnerText = btn_EditButton.Location.X.ToString();
            doc.DocumentElement.AppendChild(newElem);

            XmlElement newElemx = doc.CreateElement("y");
            newElemx.InnerText = btn_EditButton.Location.Y.ToString();
            doc.DocumentElement.AppendChild(newElemx);

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            XmlWriter writer = XmlWriter.Create("positon.xml", settings);
            doc.Save(writer);


So I want to save the location of all buttons in different child elements with the button name.

My current XML output:

XML
<?xml version="1.0" encoding="utf-8"?>
<button2>
  <x>236</x>
  <y>220</y>
</button2>
Posted
Comments
Sinisa Hajnal 19-May-15 6:26am    
To clarify: you have buttons, say btn1, btn2...btn5. You want XML with their positions saved. You have a code that seems to save single node.

So what is the problem? Why can't you do the same for the rest of the buttons?
PetarS089 19-May-15 6:28am    
When i do same to other buttons my output is

<button2>
<x>236
<y>220
<x>50
<y>45
</button2>
Sinisa Hajnal 19-May-15 7:46am    
That is because you're adding newElem to the original node. Do you know that you call your y-node newElemX :)? You have to repeat whole of the code (create button node, then two sub-nodes that you add to the button node), not just x and y parts.

Add one root node < buttons > and add button node under it. I would also advise you to create button node like this: <button name="btn1" x="valueX" y="valueY" />

but this may be just my preference.

1 solution

C#
XmlDocument doc = new XmlDocument();
            doc.LoadXml(<buttons></buttons>");
 
XmlNode btn = doc.CreateElement("button");
XmlAttribute btnName = doc.CreateAttribute("name")
btnName = btn_EditButton.Name;
btn.Attributes.Add(btnName)

            XmlElement newElem = doc.CreateElement("x");
            newElem.InnerText = btn_EditButton.Location.X.ToString();
            btn.AppendChild(newElem);
 
            XmlElement newElemx = doc.CreateElement("y");
            newElemx.InnerText = btn_EditButton.Location.Y.ToString();
            btn.AppendChild(newElemx);

doc.DocumentElement.AppendChild(btn); 

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
 
            XmlWriter writer = XmlWriter.Create("positon.xml", settings);
            doc.Save(writer);


You should get something like this:
XML
<buttons>
    <button name="btn_EditButton">
        <x>158</x>
        <y>252</y>
    </button>
</buttons>


Note that naming your button btn_EditButton is redundant, either call it btnEdit, or EditButton, don't do both.

Also, for multiple buttons, you should create a method:
C#
private XmlElement CreateButtonNode(Button btn) {
//in which you would create the button node and return it.
}

In the main part of the program you would have main document and you would simply add created nodes (one call for each button).
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900