Sandra
Sandra

Reputation: 41

Find Xml attribute by value, and change its other value

So, below I made a code which lists my 2 items in comboBox1 by its name(Selena and Maria) on load, and when I select one of those, lets say Maria, and click on button1, it populates my 3 textboxes with Maria's name,usn and pawd attribute values, and it looks like: Display name: Maria Username: mary26 Password: d4e5r

and I am happy with that part of code, because it serves my purpose.

But I am struggling with part of code which I am trying to figure out. I created a button2, and I would like that, when I change values of Display name, Username or Password textboxes, and I click save, that it saves to right location in xml file, to Maria, and does not save it to Selena or something else.

I have tried browsing for a week now, and multiple solutions, and I couldn't find any.

att.xml:

<database>
<item name="Selena" usn="sele22" pawd="fed47a"></item>
<item name="Maria" usn="mary26" pawd="d4e5r"></item>
<database>

myproject:

private void Form3_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            XmlTextReader reader = new XmlTextReader("att.xml");
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "item")
                    {
                        comboBox1.Items.Add(reader.GetAttribute("name"));
                    }
                }
            }

            reader.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string secit = comboBox1.SelectedItem as string;
            XmlTextReader lola = new XmlTextReader("att.xml");
            while (lola.Read())
            {
                if (lola.NodeType == XmlNodeType.Element)
                {
                    string poop = lola.GetAttribute("name"); 
                    if (poop == secit) 
                    {
                        string username = lola.GetAttribute("usn"); 
                        string password = lola.GetAttribute("pawd");
                        string dispname = lola.GetAttribute("name");

                        textBox1.Text = dispname;
                        textBox2.Text = username;
                        textBox3.Text = password;

                    }
                }
            }

            lola.Close();
        }

Upvotes: 2

Views: 7550

Answers (2)

bloudraak
bloudraak

Reputation: 6002

Another alternative would be to use XDocument, if you care to learn LINQ.

Lets assume you have a separate method to update the XML file, perhaps it looks like this.

private static void Update(string key, string pwd, string usn)
{
    // Enter code here to update the item
}

You can use XPath to find an element with a name:

var document = XDocument.Load("XMLFile1.xml");
var element = document.XPathSelectElement(string.Format("/database/item[@name = \"{0}\"]", key));
if (element != null)
{
    element.SetAttributeValue("usn", usn);
    element.SetAttributeValue("pawd", pwd);
    document.Save("XMLFile2.xml");
}

Or by finding the document using XDocument/XElement/LINQ methods:

var document = XDocument.Load("XMLFile1.xml");
var element = document.Elements("database")
    .Elements("item")
    .Attributes("name")
    .Where(a => a.Value == key)
    .Select(a => a.Parent)
    .SingleOrDefault();
if (element != null)
{
    element.SetAttributeValue("usn", usn);
    element.SetAttributeValue("pawd", pwd);
    document.Save("XMLFile3.xml");
}

Or you could rewrite it as a LINQ expression.

var document = XDocument.Load("XMLFile1.xml");
var elements = from e1 in document.Elements()
                where e1.Name == "database"
                from e2 in e1.Elements()
                where e2.Name == "item"
                from attribute in e2.Attributes()
                where attribute.Name == "name" && attribute.Value == key
                select e2;
var element = elements.SingleOrDefault();
if (element != null)
{
    element.SetAttributeValue("usn", usn);
    element.SetAttributeValue("pawd", pwd);
    document.Save("XMLFile3.xml");
}

Feel free to adapt accordingly.

Upvotes: 1

Casperah
Casperah

Reputation: 4554

You can use XmlDocument like this:

XmlDocument doc = new XmlDocument();
doc.Load("att.xml");
foreach(XmlNode item in doc.SelectNodes("//item"))
    comboBox1.Items.Add(item.Attributes["name"].Value);


void button3_Click(object sender, NotifyArgs e)
{
    XmlNode item = doc.SelectSingleNode("//item[@name='" + comboBox1.Text + "']");
    if (item == null) return;
    item.Attributes["name"].Value = textBox1.Text;
    ...
    doc.Save("att.xml");
}

Upvotes: 2

Related Questions