Convert xmlstring to XmlNode
I have one xml line like this
string stxml="<Status>Success</Status>";
I also created one XML document
XmlDocument doc = new XmlDocument(); XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(docNode); XmlNode rootNode = doc.CreateElement("StatusList"); doc.AppendChild(rootNode);
I need a conclusion like this.
<StatusList> <Status>Success</Status> </StatusList>
How can i achieve this. If we use innerhtml, it will be inserted. But I want to insert the xml string as xmlnode itself
A very simple way to achieve what you need is to use the often forgotten XmlDocumentFragment class:
XmlDocument doc = new XmlDocument(); XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(docNode); XmlNode rootNode = doc.CreateElement("StatusList"); doc.AppendChild(rootNode); //Create a document fragment and load the xml into it XmlDocumentFragment fragment = doc.CreateDocumentFragment(); fragment.InnerXml = stxml; rootNode.AppendChild(fragment);
Instead, you can use the XElement
class:
string stxml = "<Status>Success</Status>"; var status = XElement.Parse(stxml); var statusList = new XElement("StatusList", status); var output = statusList.ToString(); // looks as you'd like
If you want to write new statusList
content to a file:
statusList.Save(@"C:\yourFile.xml", SaveOptions.None);
using System; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; using System.IO; using System.Reflection; using System.ComponentModel; public class MyClass { public static void RunSnippet() { XmlNode node = default(XmlNode); if(node == null) Console.WriteLine(bool.TrueString); if(node != null) Console.WriteLine(bool.FalseString); XmlDocument doc = new XmlDocument(); node = doc.CreateNode (XmlNodeType.Element,"Query", string.Empty); node.InnerXml=@ "<Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where>"; string xmlData = ToXml<XmlNode>(node); Console.WriteLine(xmlData); XmlNode node1 = ConvertFromString(typeof(XmlNode), @"<Query><Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where></Query>") as XmlNode; if(node1 == null) Console.WriteLine(bool.FalseString); if(node1 != null) Console.WriteLine(bool.TrueString); string xmlData1 = ToXml<XmlNode>(node1); Console.WriteLine(xmlData1); } public static string ToXml<T>(T t) { string Ret = string.Empty; XmlSerializer s = new XmlSerializer(typeof(T)); using (StringWriter Output = new StringWriter(new System.Text.StringBuilder())) { s.Serialize(Output, t); Ret = Output.ToString(); } return Ret; } public static object ConvertFromString(Type t, string sourceValue) { object convertedVal = null; Type parameterType = t; if (parameterType == null) parameterType = typeof(string); try { // Type t = Type.GetType(sourceType, true); TypeConverter converter = TypeDescriptor.GetConverter(parameterType); if (converter != null && converter.CanConvertFrom(typeof(string))) { convertedVal = converter.ConvertFromString(sourceValue); } else { convertedVal = FromXml(sourceValue, parameterType); } } catch { } return convertedVal; } public static object FromXml(string Xml, Type t) { object obj; XmlSerializer ser = new XmlSerializer(t); using (StringReader stringReader = new StringReader(Xml)) { using (System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stringReader)) { obj = ser.Deserialize(xmlReader); } } return obj; } #region Helper methods public static void Main() { try { RunSnippet(); } catch (Exception e) { string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString()); Console.WriteLine(error); } finally { Console.Write("Press any key to continue..."); Console.ReadKey(); } } private static void WL(object text, params object[] args) { Console.WriteLine(text.ToString(), args); } private static void RL() { Console.ReadLine(); } private static void Break() { System.Diagnostics.Debugger.Break(); } #endregion }
In my experience, it is always better to work with a unique identifier, I suggest you first look in this situation, and then return to this page and examine / place my code for an attempt, if you have not had a solution for this yet, I just finished it on To my side for my own project, I changed abit to look more integrated for your project. Good luck. Sorry for the late reply; -)
XmlDocument xDoc = new XmlDocument(); string Bingo = "Identification code"; xDoc.Load(pathFile); XmlNodeList idList = xDoc.GetElementsByTagName("id"); XmlNodeList statusList = xDoc.GetElementsByTagName("Status"); for (int i = 0; i < idList.Count; i++) { StatusNode = "<Status>fail</Status>"; XmlDocumentFragment fragment = xDoc.CreateDocumentFragment(); fragment.InnerXml = StatusNode; statusList[i].InnerXml = ""; statusList[i].AppendChild(fragment); if (statusList[i].InnerText == Bingo) { StatusNode = "<Status>Succes!</Status>"; fragment.InnerXml = Status; statusList[i].InnerXml = ""; statusList[i].AppendChild(fragment); } } xDoc.Save(pathFile);