Updating XML Using Linq

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?VGVycmFuY2U=?=

    Updating XML Using Linq

    I'm trying to update an xml file but seem to be having problems. My xml file
    is in the following format:
    <main>
    <site name="mysite" username="user" password="pass" />
    <site ... />
    </main>
    The code I have is:
    XDocument mydoc =
    XDocument.Load( this.txtFilePat h.Text,LoadOpti ons.PreserveWhi tespace);

    XElement myElem = mydoc.Elements( "site").Whe re(e =(string)
    e.Attribute("na me") == fieldArray[0]).FirstOrDefaul t();

    if (myElem == null) Console.WriteLi ne("myElem is null");

    //myElem.SetAttri buteValue("name ",this.txtSite. Text);
    myElem.Attribut e("name").Val ue = this.txtSite.Te xt;
    //myElem.SetAttri buteValue("logi n", this.txtUsernam e.Text);
    myElem.Attribut e("login").Valu e = this.txtUsernam e.Text;
    //myElem.SetAttri buteValue("pass word", this.txtPasswor d.Text);
    myElem.Attribut e("password").V alue = this.txtPasswor d.Text;

    It doesn't seem to be working. myElem variable seems to be null. Any
    suggestions?
    --
    TC
  • Martin Honnen

    #2
    Re: Updating XML Using Linq

    Terrance wrote:
    I'm trying to update an xml file but seem to be having problems. My xml file
    is in the following format:
    <main>
    <site name="mysite" username="user" password="pass" />
    <site ... />
    </main>
    The code I have is:
    XDocument mydoc =
    XDocument.Load( this.txtFilePat h.Text,LoadOpti ons.PreserveWhi tespace);
    >
    XElement myElem = mydoc.Elements( "site").Whe re(e =(string)
    e.Attribute("na me") == fieldArray[0]).FirstOrDefaul t();
    >
    if (myElem == null) Console.WriteLi ne("myElem is null");
    >
    //myElem.SetAttri buteValue("name ",this.txtSite. Text);
    myElem.Attribut e("name").Val ue = this.txtSite.Te xt;
    //myElem.SetAttri buteValue("logi n", this.txtUsernam e.Text);
    myElem.Attribut e("login").Valu e = this.txtUsernam e.Text;
    //myElem.SetAttri buteValue("pass word", this.txtPasswor d.Text);
    myElem.Attribut e("password").V alue = this.txtPasswor d.Text;
    >
    It doesn't seem to be working. myElem variable seems to be null. Any
    suggestions?
    The 'site' elements are descendants of the XDocument node so either use
    mydoc.Descendan ts("site")
    or access the root element first
    mydoc.Root.Elem ents("site")

    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?VGVycmFuY2U=?=

      #3
      RE: Updating XML Using Linq

      Thanks a bunch. That worked.
      --
      TC


      "Terrance" wrote:
      I'm trying to update an xml file but seem to be having problems. My xml file
      is in the following format:
      <main>
      <site name="mysite" username="user" password="pass" />
      <site ... />
      </main>
      The code I have is:
      XDocument mydoc =
      XDocument.Load( this.txtFilePat h.Text,LoadOpti ons.PreserveWhi tespace);
      >
      XElement myElem = mydoc.Elements( "site").Whe re(e =(string)
      e.Attribute("na me") == fieldArray[0]).FirstOrDefaul t();
      >
      if (myElem == null) Console.WriteLi ne("myElem is null");
      >
      //myElem.SetAttri buteValue("name ",this.txtSite. Text);
      myElem.Attribut e("name").Val ue = this.txtSite.Te xt;
      //myElem.SetAttri buteValue("logi n", this.txtUsernam e.Text);
      myElem.Attribut e("login").Valu e = this.txtUsernam e.Text;
      //myElem.SetAttri buteValue("pass word", this.txtPasswor d.Text);
      myElem.Attribut e("password").V alue = this.txtPasswor d.Text;
      >
      It doesn't seem to be working. myElem variable seems to be null. Any
      suggestions?
      --
      TC

      Comment

      Working...