Below are my two xml files- Sample xml file
<tbltemp> <Details> <Id>1</Id> <Name>John</Name> <Date>25.09.2016</Date> <Time>09:30:45</Time> <Flag>1</Flag> </Details> <Details> <Id>2</Id> <Name>John</Name> <Date>25.09.2016</Date> <Time>01:30:45</Time> <Flag>1</Flag> </Details> <Details> <Id>3</Id> <Name>John</Name> <Date>25.09.2016</Date> <Time>11:36:45</Time> <Flag>1</Flag> </Details> <Details> <Id>1</Id> <Name>Jack</Name> <Date>25.09.2016</Date> <Time>11:36:45</Time> <Flag>1</Flag> </Details> </tbltemp>
DBfile.xml
<tbltemp> <Data> <Id>1</Id> <Name>John</Name> <Age>23</Age> <Team>Software</Team> <Flag>1</Flag> </Data> <Data> <Id>2</Id> <Name>John</Name> <Age>23</Age> <Team>Software</Team> <Flag>1</Flag> </Data> <Data> <Id>3</Id> <Name>John</Name> <Age>23</Age> <Team>Software</Team> <Flag>1</Flag> </Data> <Data> <Id>1</Id> <Name>Jack</Name> <Age>24</Age> <Team>Software</Team> <Flag>1</Flag> </Data> </tbltemp>
I want to copy Date and time in DBfile.xml file from sample xml file where Ids match for both the xml file.
I want the DBfile.xml like this-
<tbltemp> <Data> <Id>1</Id> <Name>John</Name> <Age>23</Age> <Team>Software</Team> <Date>25.09.2016</Date> <Time>09:30:45</Time> <Flag>1</Flag> </Data> <Data> <Id>2</Id> <Name>John</Name> <Age>23</Age> <Team>Software</Team> <Date>25.09.2016</Date> <Time>01:30:45</Time> <Flag>1</Flag> </Data> <Data> <Id>3</Id> <Name>John</Name> <Age>23</Age> <Team>Software</Team> <Date>25.09.2016</Date> <Time>11:36:45</Time> <Flag>1</Flag> </Data> <Data> <Id>1</Id> <Name>Jack</Name> <Age>24</Age> <Team>Software</Team> <Date>25.09.2016</Date> <Time>11:36:45</Time> <Flag>1</Flag> </Data> </tbltemp>
I have been using code like this
strDetails = "sample.xml";
strDBDir = "DBfile.xml";
var xDoc1 = XDocument.Load(strDetails);
var xDoc2 = XDocument.Load(strDBDir);
var doc1Entries = xDoc1.Descendants("Details");
/// var cloneEntries = doc1Entries.Select(x => new XElement(x));//"AgentId").Value == AgentId.ToString()
var cloneEntries = doc1Entries.Select(x => new XElement(x));
xDoc2.Descendants("Date").Last().AddFirst(cloneEntries);
xDoc2.Descendants("Time").Last().AddAfterSelf(cloneEntries);
xDoc2.Save(strDBDir);
But unable to copy data from one file to other.,Instead it is just adding loginid into DBfile.xml
Can anyone guide here please.
Using linq join
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)
{
XDocument sampleDoc = XDocument.Load(@"c:\temp\sample.xml");
XDocument dbDoc = XDocument.Load(@"c:\temp\dbfile.xml");
var merge =
(from db in dbDoc.Descendants("Data")
join sample in sampleDoc.Descendants("Details") on new { id = (int)db.Element("Id"), name = (string)db.Element("Name") } equals new { id = (int)sample.Element("Id"), name = (string)sample.Element("Name") }
select new { db = db, sample = sample });
foreach (var row in merge)
{
row.db.Add(new object[] {
row.sample.Element("Date"),
row.sample.Element("Time")
});
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.