Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the code snippet below:
VB
myValue = "hello""world & me"
myNode = xmlDoc.CreateElement("Hello")
myText = xmlDoc.CreateTextNode(myValue.replace("""","&quo t;"))
myNode.AppendChild(myText)


If for articleText = doc.CreateTextNode(myValue.replace("""","& quo t;"))
result is: hello"world & me

If for articleText = doc.CreateTextNode(myValue)
result is: hello"world & me

The requirements is that: the double quote must be encoded correctly

Notei have added spaces in the encoded value of the double quote to prevent it from conversion here in code project when preview. :)
Posted
Updated 1-Sep-11 20:51pm
v2

This is working fine for me. Try it hope it helps

C#
XmlDocument xmlDoc = new XmlDocument();
            string   myValue = '"'+ "Hello" + '"' + '"'+"World & me " +'"';
            XmlNode myNode = xmlDoc.CreateElement("Hello");
            myNode.InnerText = myValue;
            xmlDoc.AppendChild(myNode);
            xmlDoc.Save("c:/test-doc.xml");
 
Share this answer
 
Comments
Jephunneh Malazarte 2-Sep-11 12:05pm    
Hello Anuja thanks for your reply.
I never tried your code, so your suggesting to use InnerText property of the myNode instead of creating a creating a new instance of text node.

I am not sure but it looks like that will fix the issue.
I will accept the solution once i have tested it in my box :) thanks once again. :)
Anuja Pawar Indore 3-Sep-11 1:10am    
was working fine, i tried it. Hope it solves ur problem. :)
Jephunneh Malazarte 5-Sep-11 5:48am    
Hello Anuja,

I have tried it using the suggested solution. But the problem is that the double quote is not encoded.
Here's the sample output: "Hello""World & me "
Are we seeing the same output?

Currently my approach for a workaround is that after i saved the file as xml using xmldocument, i reopen the file using filestream and replace the " with @quot; i know that's not how to address the issue.. huhuhuhu...

But i am currently stock and i am curious why double quote won't be encoded using xmldocument object.
Anuja Pawar Indore 5-Sep-11 7:59am    
Yes was seeing the same output, Double quotes have a predefined meaning. So we need to find a way to enter them and of course will be indirect.
Jephunneh Malazarte 6-Sep-11 5:16am    
ahhh... i got you anuja... thanks.
I tag your suggestion as accepted :)

thank you as well for the clarification that the work around will be indirect.
I have also finalize my workaround... thanks to you that i don't doubt it anymore... hehehe :)
Thanks very much :)
C#
.
.
.
doc.Save("MySampleXML.xml");

//Open the saved xml file as text and update the " to "
System.TextStringBuilder newFile = new System.Text.StringBuilder();
string tempString = "";
string []fileString = File.ReadAllLines("MySampleXML.xml");
foreach(string line in fileString)
{
    if (line.Contains("& amp;quot;"))
    {
        tempString = line.Replace("& amp;quot;","& quot;");
        newFile.AppendLine(tempString);
        continue for;
    }
}
File.WriteAllText("MySampleXML.xml", newFile.ToString());


Note: Please remove the space between & and amp;quot;. I added space so codeproject won't encode it. Don't know how to disable it... hehehe :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900