7

I am trying to find and replace text in an xml file using c#. What I want is to change server name in the url link throughout the file.

http://Server1.extranet.abc/server1webdev/rest/services/ABC/ABC_Base/MapServer

to

http://Server2.extranet.abc/server1webdev/rest/services/ABC/ABC_Base/MapServer 

I tried using System.xml.linq (XDocument.load(xmlpath)) but it simply gives me the whole xml file as one line of string. Is there a way I can replace the text?Note that the url's are not in specific nodes., they are random throughout file. I am able to do this manually through the file's find and replace, is there a way of doing this programmatically?

4
  • 3
    What about simple String.Replace: xmlString = xmlString.Replace(oldUrl, newUrl)? Commented Sep 11, 2013 at 14:59
  • 1
    Right, if you're just doing such a simple replace, why even bother parsing it. Commented Sep 11, 2013 at 15:02
  • I have to do this for a lot of files and there might be cases where I might need to change some other text in the url, @lazyberezovsky can you please elaborate? How do I extract the xml string?it is random throughout the file not in any specific node. Thanks Commented Sep 11, 2013 at 15:06
  • 1
    @lazyberezovsky Hi Thanls alot your idea of xmlString = xmlString.Replace(oldUrl, newUrl) works. I would suggest converting xml to a string replace the content and parse it back as an xml.Cool! Commented Sep 11, 2013 at 16:36

3 Answers 3

10

if you have the entire xml file as string you can replace what you need by doing:

string oldStr = @"http://Server1.extranet.abc/server1webdev/rest/services/ABC/ABC_Base/MapServer";
string newStr = @"http://Server2.extranet.abc/server1webdev/rest/services/ABC/ABC_Base/MapServer ";

doc.Replace(oldStr, newStr);

but normally if you want to change a value of a tag in xml i can suggest an example and you put it to use in your xml:

     XDocument doc = XDocument.Load("D:\\tst.xml");
     foreach (XElement cell in doc.Element("Actions").Elements("Action"))
     {
        if (cell.Element("ActionDate").Value == oldStr)
        {
           cell.Element("ActionDate").Value = newStr;
        }
     } 

     doc.Save("D:\\tst.xml");
Sign up to request clarification or add additional context in comments.

4 Comments

Hi No Idea For Name,thanks for the reply, isnt there a way to simply replace the text? The url is placed in random elements/nodes, I cant pinpoint them.Also for the first part I couldnt find doc.Replace, there is only doc.ReplaceWith(object) and it needs the node
Secondly I want to replace just some bits of text and not the whole url, is there another way? Thanks
@GBh you won't find doc because i don't know how you called your string, but you said you have the whole file as a string, so you can call Replace on a string.
@GBh also, if you want to work with xDocument you can go recursively on your child nodes searching the value for the given url
1
List<XElement> allElements = xmlDocument.Descendants().ToList();
foreach (XElement element in allElements.Where(e => e.Value == oldstring))
{
    element.Value = newstring
}

Alternatively Where clause could have a Contains and Value could use string replace.

A side tip to preserve line numbers when loading XDocument, which is super handy if you're doing validation:

XDocument xmlDocument = XDocument.Load(xmlFile, LoadOptions.SetLineInfo);

Comments

0

Using XDocument there is currently no built-in way to replace text in the whole file. However what you can do is

XDocument document = XDocument.LoadFrom(path);
var docText = document.ToString().Replace(urlA, urlB);
using (var reader = new StringReader(docText))
    document = XDocument.Load(reader, LoadOptions.None);

Which is not ideal, but at least it's a workaround.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.