CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    25

    How can I open an XML File and replace an attribute value?

    Lets say my value looks like this in the xml file


    <<PARTNUMBER Value="352698563 " />


    How can I replace that value number?

    So far I have this

    XmlDocument doc = new XmlDocument();
    XmlNode node;


    doc.Load(filename);

    xpath = string.Format(@".//{0}[@{1}]", "PARTNUMBER" , "Value");
    node = doc.DocumentElement.SelectSingleNode(xpath);


    it successfully navigates to that node but how do I replace the value?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How can I open an XML File and replace an attribute value?

    Access the XmlAttribute from the following link, and the use its Value property to change the value.

    http://msdn.microsoft.com/en-us/library/hk61a712.aspx

  3. #3
    Join Date
    Aug 2009
    Posts
    25

    Re: How can I open an XML File and replace an attribute value?

    That tells me how to get to an attribute but doesnt tell me how to set or replace an attribute value. That is where I am stuck at?

    Anyone else have an idea?

  4. #4
    Join Date
    Aug 2009
    Posts
    25

    Re: How can I open an XML File and replace an attribute value?

    Nevermind I got it

    node.Attributes["Value"].Value = ReplacementValue;

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How can I open an XML File and replace an attribute value?

    Quote Originally Posted by codeguy03 View Post
    That tells me how to get to an attribute but doesnt tell me how to set or replace an attribute value. That is where I am stuck at?
    I gave you the link to retrieve the XmlAttribute and then I told you to set the value via the 'Value' property.

  6. #6
    Join Date
    Oct 2009
    Posts
    3

    Re: How can I open an XML File and replace an attribute value?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured