Replace element in Xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milecimm
    New Member
    • Oct 2007
    • 24

    #1

    Replace element in Xml file

    Hi Help..........! !
    I have a xml file and need to load it, read it and replace (only in memory) the content of one of its elements.

    How do I do that?????????

    Many thanks

    M

    ps. I'm using vb.net
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    A short example is provided here

    Comment

    • milecimm
      New Member
      • Oct 2007
      • 24

      #3
      Hi
      Thanks for your prompt reply.
      But I don't want to edit but to replace one of the elements innetText in the file while the file is loaded and before I apply the xpath expression (query).

      My xml file looks more or less like this:

      <Jobs>
      <category>Acade mic</category>
      <category>Acade mic related</category>
      <category>Resea rch</category>
      <category>Techn ical</category>
      </jobs>

      I want to replace Academic Related for Acad-Related because my xpath expression that queries the xml file asks for Acad-related job category instead of Academic related (long story here to tell), but I dont want to physically change or modify the xml file.
      Hope my problem is clearer this time
      Many thanks
      M

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        It'd be pretty similar to the example, just select a single node and change the text:

        [code=vb]
        // select node
        XmlNode node = xmlDoc.SelectSi ngleNode("/Jobs/category[.='Academic Related']");
        // change text of node.
        node.InnerText = "Acad-Related";
        [/code]

        Comment

        • milecimm
          New Member
          • Oct 2007
          • 24

          #5
          Thanks for your help but I dont think your answer is solving my problem.Here there is a more detailed explanation:

          Xml file:
          Code:
          <Jobs>
          <category>
          <categoryname>Academic</categoryname>
          <categoryid>1</categoryid>
          </category>
          <category>
          <categoryname>Academic related</categoryname>
          <categoryid>2</categoryid>
          </category>
          <category>
          <categoryname>Research</categoryname>
          <categoryid>3</categoryid>
          </category>
          <category>
          <categoryname>Technical</categoryname>
          <categoryid>4</categoryid>
          </category>
          </Jobs>
          Vb.net:
          Code:
          Dim getCategory As String = 'here I get the category name that user selects. The category selected could be:::  Acad-related'
          Dim doc As XmlDocument = New XmlDocument
          doc.Load(xml_file)
          Dim xPath As String = "jobs/category[categoryname = " & getCategory & "]"
          
          'AS YOU CAN SEE FROM THE XML FILE A CATEGORY IS CALLED 'Academic related' BUT USERS COULD SELECT 'Acad-related' FROM THE DROP-DOWN LIST AVAILABLE TO THEM. I NEED TO CHANGE THAT ELEMENT AFTER I LOAD THE FILE AND  BEFORE THE XPATH QUERY IS APPLIED. OTHERWISE I WOULD BE GETTING NO RESULTS AS THE XML FILE HAS NO Acad-related CATEGORY. I SHOULD NOT EDIT THE XML FILE SO THE CHANGE HAS TO BE MADE ONLY IN MEMORY
          
          Dim nodes As XmlNodeList = doc.SelectNodes(xPath)
          If nodes.Count <> 0 Then
          	For Each item In nodes
          	Dim cat_id As XmlNode = item.SelectSingleNode("categoryid")
          	Dim cat_id_val As String = cat_id.InnerText
          	Dim cat_name As XmlNode = item.SelectSingleNode("categoryname")
          	Dim cat_name_val As String =cat_name.InnerText
          	response.write ("Our job categories are:" & "<br>")
          	response.write(cat_name_val & " ("  & cat_id_val & ")" & "<br>")
          	Next
          end if
          Many thanks again
          M

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            The xpath provided earlier is case sensitive. Would have to change to
            XmlNode node = xmlDoc.SelectSi ngleNode("/Jobs/category[.='Academic related']")

            Are you not allowed to change the getCategory like so (before creating the xPath)?

            If getCategory = "Acad-related" Then
            getCategory = "Academic related"
            end if

            Or if your problem is with changing the xml file, have you tried cloning the xml data? What is the problem with the previous solution?

            Comment

            • milecimm
              New Member
              • Oct 2007
              • 24

              #7
              I cannot change getCategory.

              I have to change the element in the xml file before I can apply the xpath, because this xpath queries for "Academic related" jobs, NOT "Acad-related" jobs. And getCategory IS "Acad-related"!!

              MY XPATH:
              Dim xPath As String = "jobs/category[categoryname = " & getCategory & "]"

              Thanks

              M

              Comment

              Working...