Peter has already shown you how to do it, but here is an example:
Suppose your end game is this:
<?xml version="1.0"?>
<users>
<user>
<id>1</id>
<name>Greg</name>
</user>
</users>
And you are building the individual users in a loop. So, let's create a
testable method that returns a single user, knowing we are going to loop
later:
public static string CreateUsersAsXml()
{
//pretend we are pulling from database
int id = 1;
string name = "Greg";
StringBuilder builder = new StringBuilder();
//tabs and returns for readability
builder.Append("<user>");
builder.Append("<id>");
builder.Append(id);
builder.Append("</id>");
builder.Append("<name>");
builder.Append(name);
builder.Append("</name>");
builder.Append("</user>");
return builder.ToString();
}
You can now test to make sure it returns:
<user><id>1</id><name>Greg</name></user>
[TestMethod]
public void DoesXmlCreatorReturnCorrectXmlStringTest()
{
string expected = "<user><id>1</id><name>Greg</name></user>";
string actual = MyXmlCreator.CreateUsersAsXml();
Assert.AreEqual(expected, actual, "Xml String are not equal");
}
Now, to your problem, the \" (which is not a problem). Put this in a console
application and do the following:
public static string GetXmlUsers()
{
string myXml = MyXmlCreator.CreateUsersAsXml();
//Should use a StringBuilder here
string xml = "<?xml version=\"1.0\"?><users>" + myXml + "</users>";
StringReader reader = new StringReader(xml);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
return doc.InnerXml.ToString();
}
Output, as expected is:
<?xml version="1.0"?><users><user><id>1</id><name>Greg</name></user></users>
Here is the entire program in a Console Application so you can test it:
class Program
{
static void Main(string[] args)
{
string myXml = CreateUsersAsXml();
string xml = "<?xml version=\"1.0\"?><users>" + myXml + "</users>";
StringReader reader = new StringReader(xml);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode node = doc.SelectSingleNode("users/user/id");
Console.WriteLine(node.FirstChild.Value);
Console.Write(doc.InnerXml.ToString());
Console.Read();
}
public static string CreateUsersAsXml()
{
//pretend we are pulling from database
int id = 1;
string name = "Greg";
StringBuilder builder = new StringBuilder();
//tabs and returns for readability
builder.Append("<user>");
builder.Append("<id>");
builder.Append(id);
builder.Append("</id>");
builder.Append("<name>");
builder.Append(name);
builder.Append("</name>");
builder.Append("</user>");
return builder.ToString();
}
}
Note that the \" is equivalent to "" when used in this manner. Also note
that this works, as the XML is properly loaded into an XML document and you
can even consume as XML. This is proven in the statement:
XmlNode node = doc.SelectSingleNode("users/user/id");
Console.WriteLine(node.FirstChild.Value);
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss
or just read it:
http://gregorybeamer.spaces.live.com/
********************************************
| Think outside the box! |
********************************************
"xzzy" <mr********@comcast.netwrote in message
news:y4******************************@comcast.com. ..
>
None of the following properly do the VB.net double quote conversion
because all of the following in csharp convert to
\" instead of just a double quote: "
I have tried:
char myDoubleQuote = (char)34;
string myDoubleQuote = "" + (char)34;
string myDoubleQuote = "\"";
myItems = "<?xml version=" + myDoubleQuote + "1.0" + myDoubleQuote
+ "?><users>" & myXml & "</users>"
I even tried:
myItems = "<?xml version=<![CDATA["]]>1.0<![CDATA["]]>?><users>" &
myXml & "</users>"
it does not compile.
I need the string myItems to be able to properly handle the creation of
one double quote on each side of 1.0 :
myItems = <?xml version="1.0"?><users+ myXml + "</users>
Thank you for your help with this problem!