23

How can we write an XML file into a string variable? Here is the code I have,the variable content is supposed to return an XML string:

    public string GetValues2()
    {
        string content = "";
        XmlTextWriter textWriter = new XmlTextWriter(content, null);
        textWriter.WriteStartElement("Student");
        textWriter.WriteStartElement("r", "RECORD", "urn:record");
        textWriter.WriteStartElement("Name", "");
        textWriter.WriteString("Student");
        textWriter.WriteEndElement();
        textWriter.Close();

        return contents;

    }
5
  • When I try this the program says that I need to define a path instead of content Commented Apr 9, 2013 at 7:18
  • do you want to read xml file into string. am I correct? Commented Apr 9, 2013 at 7:20
  • 1
    If you want create xml file and then assign it to string variable use Linq 2 Xml it's the fastest way. Commented Apr 9, 2013 at 7:22
  • @Sachin:No,I want to generate an XML file and save it into a string variable,I must not save any files or use any files from the hard disk,I appreciate your help Commented Apr 9, 2013 at 7:22
  • @harry180:thanks,but how can I do that? Commented Apr 9, 2013 at 7:23

4 Answers 4

71

Something like this

string xmlString =  System.IO.File.ReadAllText(fileName);

Here is good answer to create XmlDocument XDocument or XMLDocument

Sign up to request clarification or add additional context in comments.

1 Comment

Be aware that File.ReadAllText will lock the file. If you do not wish to lock the file you should use a FileStream with a StreamReader (in a using statement). (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)). The same remark if you use XmlDocument.Load() this will also lock the file. To avoid use the stream alternative.
4

You can try:

static string GetXmlString(string strFile)
{
    // Load the xml file into XmlDocument object.
    XmlDocument xmlDoc = new XmlDocument();
    try
    {
        xmlDoc.Load(strFile);
    }
    catch (XmlException e)
    {
        Console.WriteLine(e.Message);
    }
    // Now create StringWriter object to get data from xml document.
    StringWriter sw = new StringWriter();
    XmlTextWriter xw = new XmlTextWriter(sw);
    xmlDoc.WriteTo(xw);
    return sw.ToString();
}

or just use XmlDocument.InnerXml property to get XML string.

XmlDocument doc = new XmlDocument();
doc.Load("path to your file");
string xmlcontents = doc.InnerXml;

Comments

2

Try this-

XmlDocument doc = new XmlDocument();
doc.LoadXml(your text string);

StringBuilder sb = new StringBuilder();
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
    sb.Append(char.ToUpper(node.Name[0]));
    sb.Append(node.Name.Substring(1));
    sb.Append(' ');
    sb.AppendLine(node.InnerText);
}
return sb;

have a look on this too-

    StringWriter sw = new StringWriter();
    XmlTextWriter tx = new XmlTextWriter(sw);
    myxml.WriteTo(tx);

    string str = sw.ToString();// 
    return str;

and if you really want to create a new XmlDocument then do this

XmlDocument newxmlDoc= myxml

1 Comment

This is literally a direct copy of the answer posted by @Kimtho6, here: stackoverflow.com/a/6161182/12068778
2

HI Pedram You can Try the below code

XmlDocument doc = new XmlDocument();

doc.LoadXml("yourXMLPath");
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
doc.WriteTo(tx);
sw.ToString();

1 Comment

Thanks but I want to generate an XML file and save into the a string variable,I must not load or save files into or from hard disk

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.