Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an XML file to string type

Tags:

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;      } 
like image 857
Pedram Avatar asked Apr 09 '13 07:04

Pedram


2 Answers

Something like this

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

Here is good answer to create XmlDocument XDocument or XMLDocument

like image 146
Sachin Avatar answered Oct 14 '22 17:10

Sachin


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 
like image 32
Sandy Avatar answered Oct 14 '22 15:10

Sandy