Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml nodes editing based on an xmlElement

Tags:

c#

xml

asp.net

I have tried two ways but they both didnt work.. the first way::

string filepath = Server.MapPath[this is not a link]("XMLFile2.xml");
XmlDocument xdoc = new XmlDocument();
                xdoc.Load(filepath);

                XmlNode root = xdoc.DocumentElement;
                XmlNode idNode = root.SelectSingleNode("/students/student/id");
if (idNode.Value == 9.ToString())
                    {
                        var nodeOfStudent = xdoc.SelectNodes("/students/student[@id='9']");
                        nodeOfStudent[1].InnerXml = TextBox_firstname.Text;
                        nodeOfStudent[2].InnerXml = TextBox_lastname.Text;
                        nodeOfStudent[3].InnerXml = TextBox_dob.Text;
                        nodeOfStudent[4].InnerXml = TextBox_class.Text;
                        nodeOfStudent[5].InnerXml = TextBox_section.Text;
                        nodeOfStudent[6].InnerXml = TextBox_telephone.Text;

                    }

the second way::

string filepath = Server.MapPath("XMLFile2.xml");
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(filepath);

                XmlNode root = xdoc.DocumentElement;
                XmlNode idNode = root.SelectSingleNode("/students/student/id");

xdoc.SelectSingleNode("/students/student[@id='10']/firstname").InnerXml = TextBox_firstname.Text;
                        xdoc.DocumentElement.AppendChild(firstname);
                        xdoc.SelectSingleNode("/students/student[@id='10']/lastname").InnerXml = TextBox_firstname.Text;
                        xdoc.SelectSingleNode("/students/student[@id='10']/dob").InnerXml = TextBox_firstname.Text;
                        xdoc.SelectSingleNode("/students/student[@id='10']/class").InnerXml = TextBox_firstname.Text;
                        xdoc.SelectSingleNode("/students/student[@id='10']/section").InnerXml = TextBox_firstname.Text;
                        xdoc.SelectSingleNode("/students/student[@id='10']/telephone").InnerXml = TextBox_firstname.Text;

                        xdoc.Save(filepath);

is there something wrong i dont see... my xml file looks like this::

<students>
  <student>
    <id>1</id>
    <first_name>ahmad</first_name>
    <last_name>hani</last_name>
    <DOB>12/5/1998</DOB>
    <class>sixth</class>
    <section>A</section>
    <telephone>06555632</telephone>
  </student>  
</students>

and i used a query string to load the values from a gridView located in another page using "QueryString"....

thanks in advance.

like image 247
Hammam Muhareb Avatar asked Nov 12 '22 16:11

Hammam Muhareb


1 Answers

You can easily do this with Linq to xml:

int id = 9;
XDocument xdoc = XDocument.Load(filepath);
var student = xdoc.Descendants("student")
                  .Where(s => (int)s.Element("id") == id)
                  .SingleOrDefault();

if (student != null)
{
    student.Element("first_name").Value = TextBox_firstname.Text;
    student.Element("last_name").Value = TextBox_lastname.Text;
    student.Element("DOB").Value = TextBox_dob.Text;
    student.Element("class").Value = TextBox_class.Text;
    // etc
}

xdoc.Save(filepath);
like image 190
Sergey Berezovskiy Avatar answered Nov 15 '22 04:11

Sergey Berezovskiy