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;

    }
Pedram
  • 728
  • 3
  • 10
  • 29

4 Answers4

71

Something like this

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

Here is good answer to create XmlDocument XDocument or XMLDocument

Community
  • 1
  • 1
Sachin
  • 40,216
  • 7
  • 90
  • 102
  • 2
    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. – Jeroen VL Apr 10 '19 at 07:53
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;
Viktor
  • 380
  • 5
  • 14
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();
Muhammad Naderi
  • 3,090
  • 3
  • 24
  • 37
Chandru velan
  • 136
  • 1
  • 3
  • 21
  • 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 – Pedram Apr 09 '13 at 07:25
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
Sandy
  • 2,429
  • 7
  • 33
  • 63
  • This is **literally a direct copy** of the answer posted by @Kimtho6, here: https://stackoverflow.com/a/6161182/12068778 – Momoro Apr 30 '20 at 23:01