Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

string str= "<NewDataSet> <Table1> <ID>1</ID> <Name>Tarun</Name> <Alias /> <Alias2 /> <Action>Add</Action> </Table1> <Table1> <ID>2</ID> <Name>Rahul</Name> <Alias /> <Alias2 /> <Action>Add</Action></Table1></NewDataSet>"


DataSet ds = new DataSet();
ds.ReadXml(new StringReader(str));
DataTable table1 = ds.Tables["Table1"];
         private static XDocument XMLDocumentToXDocument(XmlDocument doc)
        {
            return XDocument.Parse(doc.OuterXml);
        }

 public static string RemoveNamespace(XmlDocument xdoc)
        {
            // Regex to search for either xmlns:nsN="..." or xmlns="..."
            const string pattern = @"xmlns:?(\w+)?=""[\w:/.]+""";
            string xml = xdoc.OuterXml;

            Regex replace = new Regex(pattern);
            // Replace all occurances of the namespace declaration
            string temp = replace.Replace(xml, String.Empty);
            foreach (Match match in replace.Matches(xml))
            {
                // Loop through each Match in the Regex, this gives us the namespace aliases found
                if (match.Success && match.Groups.Count > 1)
                {
                    if (!String.IsNullOrEmpty(match.Groups[1].Value))
                    {
                        Regex alias = new Regex(@"\b" + match.Groups[1].Value + ":");
                        // Replace all occurances of the
                        temp = alias.Replace(temp, String.Empty);
                    }
                }
            }
            return temp;
        }
xml1.xml
----------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<AllNodes>
   <NodeA>
      <NodeB>test1</NodeB>
      <NodeB>test2</NodeB>
   </NodeA>
</AllNodes>





xml2.xml
-------------------



<?xml version="1.0" encoding="utf-8"?>
<AllNodes>
   <NodeA>
      <NodeB>test6</NodeB>
      <NodeB>test7</NodeB>
   </NodeA>
   <NodeA>
      <NodeB>test99</NodeB>
      <NodeB>test23</NodeB>
   </NodeA>
</AllNodes>



using LINQ to XML



var xml1 = XDocument.Load("xml1.xml");
var xml2 = XDocument.Load("xml2.xml");

//Combine and remove duplicates
var combinedUnique = xml1.Descendants("AllNodes")
                          .Union(xml2.Descendants("AllNodes"));

//Combine and keep duplicates
var combinedWithDups = xml1.Descendants("AllNodes")
                           .Concat(xml2.Descendants("AllNodes"));




Previous PostOlder Posts Home