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; }
Something like this
string xmlString = System.IO.File.ReadAllText(fileName);
Here is good answer to create XmlDocument
XDocument or XMLDocument
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With