Brendan Vogt
Brendan Vogt

Reputation: 26018

Replacing double backslash with one blackslash not working

I am busy creating an XDocument object. In 1 of the elements I need to add the domain name and service account. The service account comes in like:

MyDomainName\\MyServiceAccount

I need the tag to look like:

<ChangeRunAsName>MyDomainName\MyServiceAccount</ChangeRunAsName>

It doesn't matter how I try to replace the \\ with \, it still comes out as \\.

Here is what I currently have:

XDocument xDocument = new XDocument(
     new XDeclaration("1.0", "utf-8", null),
     new XElement("MyAppsTable",
          myApplications.Select(component => new XElement("MyApps",
               new XElement("ChangeResult", string.Empty),
               new XElement("ChangeRunAsName", serviceAccount.DomainServiceAccount.Replace("\\\\", "\\")
          ))
     )
);

The myApplications and serviceAccount input parameters look like this:

IEnumerable<MyApplication> myApplications
ServiceAccount serviceAccount

I have tried the following:

serviceAccount.DomainServiceAccount.Replace("\\\\", "\\")
serviceAccount.DomainServiceAccount.Replace(@"\\", @"\")

...and it still comes out as:

<ChangeRunAsName>MyDomainName\\MyServiceAccount</ChangeRunAsName>

I'm not sure what to do anymore.

I have this after the code above:

string xml = xDocument.ToString();

When debugging I look at the contents of xml and then I see \ and \\. This xml string I need to pass on to another method.

Upvotes: 1

Views: 2195

Answers (3)

Matthew Watson
Matthew Watson

Reputation: 109567

You are correctly replacing the backslashes.

However, when you view the results in the Visual Studio Debugger, it is escaping the backslashes (adding extra backslashes) which is giving you the impression that it didn't work.

To see the actual string in the debugger, you must use the "Text Visualizer".

To do so from the "Autos and locals" display: Look closely to the right of the displayed string and you'll see a little magnifying glass. Select the little drop arrow next to it then click "Text Visualizer". This will display the text without additional backslashes.

You can also do this if you view the variable from Quickwatch (where you right-click a variable and select "Quickwatch"). The same little magnifying glass icon with a drop arrow next to it will appear, and you can click the drop arrow and select "Text Visualizer".

Upvotes: 2

Matt
Matt

Reputation: 27001

I assume that the issue is how the DomainServiceAccount is implemented. Because you didn't post this detail, I have made some assumptions and have defined the missing classes as follows

class MyApplication { public ServiceAccount component { get; set; } }
class ServiceAccount { public string DomainServiceAccount { get; set; } }

Then I have created the following code in LinqPad:

static void Main()
{
    IEnumerable<MyApplication> myApplications=
        new System.Collections.Generic.List<MyApplication>();
    ServiceAccount serviceAccount=new ServiceAccount();
    serviceAccount.DomainServiceAccount=@"test\\account";
    ((List<MyApplication>)myApplications).Add(new MyApplication() { 
        component=serviceAccount });
    XDocument xDocument = new XDocument(
        new XDeclaration("1.0", "utf-8", null),
        new XElement("MyAppsTable",
                myApplications.Select(component => new XElement("MyApps",
                    new XElement("ChangeResult", string.Empty),
                    new XElement("ChangeRunAsName", 
                        serviceAccount.DomainServiceAccount.Replace("\\\\", "\\"))
                    )
                )
        )
    );
    xDocument.Dump();   
}

This produces the following output:

<MyAppsTable>
  <MyApps>
    <ChangeResult></ChangeResult>
    <ChangeRunAsName>test\account</ChangeRunAsName>
  </MyApps>
</MyAppsTable>

As you can see, there is only one \ as you require to have it. You can find LinqPad here: Download link (in case you want to try it out in the same environment I have used, it is usually quicker to try out code snippets in LinqPad than in Visual Studio because you don't need to create a project first).

Update: I have tried it out with Visual Studio 2010 Ultimate as well to see if there are any differences. So I've created a console application, replaced the xDocument.Dump(); statement with

string xml = xDocument.ToString();

and created a breakpoint there - as you did it. When the break point was hit, I've viewed the xml string in the XML visualizer and got the same result as in LinqPad (as shown above). The text visualizer showed the same result (with only one backslash).

Upvotes: 0

Patrick D&#39;Souza
Patrick D&#39;Souza

Reputation: 3573

I have prepared the following example. Please compare the debug values of your code with the one i have provided. By default, domain names are saved in XML as you would have liked in the "domain\username" format.

XDocument xdoc = new XDocument();
xdoc.Add(new XElement("user",System.Security.Principal.WindowsIdentity.GetCurrent().Name));
xdoc.Save(@"d:\test.xml",SaveOptions.None);

Upvotes: 0

Related Questions