-1

I have a dictionary in my project

IDictionary dict<string,string>

The key of this dictionary has xml data(complete). I want to merge all this into a single xml file and store it in another dictionary

 dict<xmldata1,null>
 dict<xmldata2,null> 

and so on i want to have dict<xml1+xml2+....,null> and the resultant xml should be parseable.

I am coding in c# .net framework

  • So, you want to concat the xml data into one string? – Daniel Marín Jan 31 '17 at 16:15
  • 2
    Ok, what is your question? What have you tried? Why are you storing your XML as a *key* in a dictionary to begin with? – tnw Jan 31 '17 at 16:17
  • @DanielMarín yes i want to concatenate but the resultant xml should be parseable. – Subramani Jan 31 '17 at 16:18
  • @tnw my dictionary contains four keys and the data inside the key are xml elements. Each of this key individually is a parseable xml file. I want to concatenate or append all this into a single key of another dictionary and i want that key to be parseable. – Subramani Jan 31 '17 at 16:20
  • That doesn't answer any of my questions. Please re-read my comment. – tnw Jan 31 '17 at 16:21
  • @tnw everytime i send one xml data to partner system i get a response message from them ,now i have decided to combine four to five xml so that i can get response for five xml at one shot.I have tried appending all the keys into one but the xml becomes unparseable(it didnt render while opening in IE OR Chrome) because of meta data of xml and tags just gets appended. – Subramani Jan 31 '17 at 16:26
  • Show your effort/code so far and be more specific about what went wrong. – tnw Jan 31 '17 at 16:27
  • Ok, I think I get it. First of all, you need to convert the XML document to string first, see [this question](http://stackoverflow.com/questions/2407302/convert-xmldocument-to-string) to get it done. – Daniel Marín Jan 31 '17 at 16:28
  • @DanielMarín thank you but that didnt solve my problem lemme give some more detail. dictis a dictionary and it has two keys and two values(all values are null).key1=<>hi and key 2 is similar to key1. All this are stored as strings in the dictionary. Now i want the dictionary to have a key which should contain this hiHello – Subramani Jan 31 '17 at 16:42
  • Ok, I will try to create a solution for you, but you should put that information in the original question. – Daniel Marín Jan 31 '17 at 16:48

3 Answers3

-1

If i understand correctly, You are looking for a method to merge your xml docs. this can be achieved with XDocument

var mergedXml = XDocument.Parse(xml1_string);
mergedXml.Root.Add(XDocument.Parse(xml2_string).Root.Elements());

continue with the rest of you xmls the same way.

At the end take the mergedDocument.ToString() and add it as your new key.

Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
-1

First, I would merge the XMLs. So, assuming, that the all XMLs share a common root element, you could use the next method to merge them:

private XmlDocument mergeXML(XmlDocument xmlDoc1, XmlDocument xmlDoc2)
{
     foreach (XmlNode node in xmlDoc2.DocumentElement.ChildNodes)
     {
         XmlNode imported = xmlDoc1.ImportNode(node, true);
         xmlDoc1.DocumentElement.AppendChild(imported);
     }
     return xmlDoc1;
 }

Then, you can use the OutterXML property to get the string content of the resultant XML, like this:

XmlDocument xmlDoc1 = new XmlDocument();
XmlDocument xmlDoc2 = new XmlDocument();

xmlDoc1.LoadXml("<?xml version='1.0'?><field><subfield1>hi</subfield1></field>");
xmlDoc2.LoadXml("<?xml version='1.0'?><field><subfield2>hello</subfield2></field>");

XmlDocument mergedXML = mergeXML(xmlDoc1, xmlDoc2);

Dictionary<string, string> XMLDictionary = new Dictionary<string, string> {
                {mergedXML.OuterXml, "whatever" }
            };

Of course, the resultant string is parseable again to XMLDocument, you could check it using linQ to access the key:

XmlDocument resultXML = new XmlDocument();
resultXML.LoadXml(XMLDictionary.Keys.ElementAt(0));
Daniel Marín
  • 1,369
  • 14
  • 36
-1

Using xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary<string,string> dict = new Dictionary<string,string>() {
                {"subfield", "hi"},
                {"subfield2", "Hello"}
            };

            string header = "<?xml version=\"1.0\"?><field></field>";
            XDocument doc = XDocument.Parse(header);
            XElement field = (XElement)doc.FirstNode;

            foreach(string key in dict.Keys)
            {
                field.Add(new XElement(key, dict[key]));
            }

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20