Search Unity

Replacing a string in a xml doc with C#

Discussion in 'Scripting' started by Artifacta, Feb 3, 2012.

  1. Artifacta

    Artifacta

    Joined:
    Mar 14, 2011
    Posts:
    22
    What I want to do is to replace a string in a xml doc. right now my xml doc looks like this:

    Code (csharp):
    1. <?xml version="1.0"?>
    2. <Data>
    3.   <Player id="1">
    4.     <name>TestName</name>
    5.   </Player>
    6. </Data>
    What I want to do now, is to replace TestName with another string.

    I have a method that looks like this, but I'm not sure if I'm going in the right direction, could really use some help getting this right.

    Code (csharp):
    1. void WriteXML(string xmlData)
    2. {
    3.  
    4.     string name = "NewPlayer";
    5.     XmlDocument xmlDoc = new XmlDocument();
    6.     xmlDoc.Load(new StringReader(xmlData));
    7.     XmlNode node = xmlDoc.SelectSingleNode("data/player/name");
    8.  
    9.     //DO something to replace the innnertext of the node with a new string
    10.     // It is here I could use some help.
    11.  
    12.     xmlDoc.Save(xmlData);
    13.  
    14.  }
    Some help would really be appriciated. Thanks in advance.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
  3. Artifacta

    Artifacta

    Joined:
    Mar 14, 2011
    Posts:
    22
    I know thats what I should do. But I'm not sure how to do it. if I just do node.InnerText = "NewName".. I get an exception object not set to an instance of an object....
     
  4. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Its not funny, its fantastic. People looking for solutions before asking!
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    touche!

    Well that just means your node is null, ie. you havnt actually found the node correctly..
     
  6. Artifacta

    Artifacta

    Joined:
    Mar 14, 2011
    Posts:
    22
    Ok, I fixed that issue, I changed the innertext of the node. But how do I write the new node back to the xml file? When I change the nod like that it just changes the innertext of the node but I guess I need to write the info in the xml file before I can save it.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    Did you look at the file after this line?

    xmlDoc.Save(xmlData);

    I dont see why that wouldnt have saved the change, of course I am assuming that xmlData is a path.
     
  8. unussystems

    unussystems

    Joined:
    Jul 15, 2019
    Posts:
    49
    Hello guys,

    I am having similar problem like above mentioned.

    Code (CSharp):
    1. public static string XmlMessageWriter(string xmlData, string[] xmlNodeNames, List<string> xmlNewValues)
    2.     {
    3.         string data = xmlData;
    4.         Debug.Log("Before Editing" + data);
    5.  
    6.         XmlDocument doc = new XmlDocument();
    7.         doc.LoadXml(data);
    8.  
    9.         for (int i = 0; i < xmlNodeNames.Length; i++)
    10.         {
    11.          
    12.             // edit xml child nodes with new values.  
    13.             doc.SelectSingleNode("//" + xmlNodeNames[i]).InnerXml = xmlNewValues[i];
    14.            // Child nodes names and new values come from method parameter above
    15.                    
    16.         }
    17.         Debug.Log("After Editing" + data);
    18.         return data;
    19.     }
    This method does 3 things:
    1. get xml data as a parameter
    2. edit its values
    3. return back after editing.

    Problem is returned variable was not edited even though it was edited in the for loop.
    I think I am using xml string not xml file. I found some solutions but all of them for xml file not xml string (xml file can be saved by using xml.Save method).

    Can anybody suggest any solutions please?
     
  9. unussystems

    unussystems

    Joined:
    Jul 15, 2019
    Posts:
    49
    This is the solutions for question above which I asked:

    https://stackoverflow.com/questions/6161159/converting-xml-to-string-using-c-sharp
     
    Last edited: Oct 15, 2019
unityunity