join 2 xmls based on a condition [Resolved]

Posted by Neerajsinghmail under C# on 12/17/2015 | Points: 10 | Views : 1503 | Status : [Member] | Replies : 6
Hello Team,

I am trying to merge 2 xml in C# based on a common condition but unable to do it.

Please refer the problem given below-

XML1 -

<DocumentElement>
<Employee>
<Depid>9228590</Depid>
<Name>John</Name>
</Employee>
<Employee>
<DepId>9295426</Depid>
<Name>Peter</Name>
</Employee>
</DocumentElement>



XML 2 -

<DocumentElement>
<Department>
<DepName>HR</DepName>
<Depid>9228590</Depid>
</Department>
<Department>
<DepName>Finance</DepName>
<Depid>9295426</Depid>
</Department>
</DocumentElement>


FINAL XML should come as below-

<DocumentElement>
<Employee>
<Depid>9228590</Depid>
<Name>John</Name>
<DepName>HR</DepName>
</Employee>
<Employee>
<DepId>9295426</Depid>
<Name>Peter</Name>
<DepName>Finance</DepName>
</Employee>
</DocumentElement>



Please help me to come out of this problem.

Thanks,
NK




Responses

Posted by: Rajnilari2015 on: 12/17/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Please refer to this article of mine( http://www.dotnetfunda.com/articles/show/2269/return-an-anonymous-collection-from-a-method-using-expando-object ) which address the same problem.
Hope this helps

--
Thanks & Regards,
RNA Team

Neerajsinghmail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/17/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
1
Down
First thing is that, your XML formation is wrong..May be a typo. I have modified that as under

Employee.xml
<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
<Employee>
<DepId>9228590</DepId>
<Name>John</Name>
</Employee>
<Employee>
<DepId>9295426</DepId>
<Name>Peter</Name>
</Employee>
</DocumentElement>


Department.xml

<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
<Department>
<DepName>HR</DepName>
<Depid>9228590</Depid>
</Department>
<Department>
<DepName>Finance</DepName>
<Depid>9295426</Depid>
</Department>
</DocumentElement>


And the program

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<ExpandoObject> EmpInformation = GetEmpInformation();
List<ExpandoObject> DeptInformation = GetDeptInformation();

//join between EmpInformation and DeptInformation
var recordSet = (
from emp in EmpInformation
join dept in DeptInformation on
(string)((IDictionary<string, object>)emp)["DeptId"] equals
(string)((IDictionary<string, object>)dept)["DeptId"]

select new
{
Depid = (string)((IDictionary<string, object>)emp)["DeptId"],
Name = (string)((IDictionary<string, object>)emp)["EmployeeName"],
DepName = (string)((IDictionary<string, object>)dept)["DepartmentName"]

}).ToList();

XDocument doc =
new XDocument(
new XElement("DocumentElement",
recordSet.Select
(
s =>
new XElement("Employee",

new XElement("Depid", s.Depid),
new XElement("Name", s.Name),
new XElement("DepName", s.DepName))
)));

}

private static List<ExpandoObject> GetEmpInformation()
{
try
{
//Load xml
XDocument xdoc = XDocument.Load(@"D:\Employee.xml");
if (xdoc == null) return null;

var empDetails = xdoc.Descendants("Employee")
.Select(empDetail =>
{
dynamic expandoObj = new ExpandoObject();

expandoObj.DeptId = empDetail.Element("DepId").Value;
expandoObj.EmployeeName = empDetail.Element("Name").Value;

return (ExpandoObject)expandoObj;

}).ToList();

return empDetails;

}
catch (Exception ex)
{
throw ex;
}
}

private static List<ExpandoObject> GetDeptInformation()
{
try
{
//Load xml
XDocument xdoc = XDocument.Load(@"D:\Department.xml");
if (xdoc == null) return null;

var deptDetails = xdoc.Descendants("Department")
.Select(deptDetail =>
{
dynamic expandoObj = new ExpandoObject();

expandoObj.DeptId = deptDetail.Element("Depid").Value;
expandoObj.DepartmentName = deptDetail.Element("DepName").Value;

return (ExpandoObject)expandoObj;

}).ToList();

return deptDetails;

}
catch (Exception ex)
{
throw ex;
}
}
}
}


hope this helps


--
Thanks & Regards,
RNA Team

Neerajsinghmail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neerajsinghmail on: 12/23/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks you very much for your response.

It resolved my problem.

Thanks,
NK

Neerajsinghmail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Neerajsinghmail Sir, Glad that it helped you. Shortly I will write an article on that. One more approach to do the same is to use Tuple instead of Expando Object. (:

--
Thanks & Regards,
RNA Team

Neerajsinghmail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 3/10/2016 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
@Neerajsinghmail here are the series of Articles Rajnilari2015 has written on Tuples. Great series to learn about Tuples in C#. Thanks Rajnilari2015 - http://www.dotnetfunda.com/articles/show/3243/let-us-learn-tuple-part1



Regards,
Sheo Narayan
http://www.dotnetfunda.com

Neerajsinghmail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 3/10/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Sheonarayan Sir, thanks a lot

--
Thanks & Regards,
RNA Team

Neerajsinghmail, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response