string xmlstr1 = @"<root><head>myHead</head><body></body></root>"; string xmlstr2 = @"<so><num>1</num><sodetail><num>1</num></sodetail></so>"; XmlDocument doc1 = new XmlDocument(); doc1.LoadXml(xmlstr1); XmlDocument doc2 = new XmlDocument(); doc2.LoadXml(xmlstr2); object a= doc2.FirstChild; XmlNode ch = doc1.ImportNode(doc2.DocumentElement, true); XmlNode node1 = doc1.SelectSingleNode("/root/body"); node1.AppendChild(ch);
private static string ConvertToXML<T>(T msg) { XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add("", ""); System.Xml.Serialization.XmlSerializer xmlser = new System.Xml.Serialization.XmlSerializer(typeof(T)); StringBuilder sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings { OmitXmlDeclaration = true })) { xmlser.Serialize(writer, msg, xmlnsEmpty); } //System.Threading.Thread.Sleep(5000); return sb.ToString(); } private static T ConvertToObject<T>(string xmlstr) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (XmlReader reader = new XmlTextReader(new StringReader(xmlstr))) { return (T)xmlSerializer.Deserialize(reader); } }