TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
darma teja
NA
496
336.9k
merge all XML file in a folder in C#
Aug 18 2016 5:41 AM
I want to merge all xml files in a folder. Here i am giving sample two xml files:
My first xml file is like this:
<
first
>
<
second
>
<
third
>
123
</
third
>
<
text
>
<
type
>
text1
</
type
>
<
country
>
fr
</
country
>
<
vaue
>
No1
</
value
>
</
text
>
<
third
>
345
</
third
>
<
text
>
<
type
>
text2
</
type
>
<
country
>
fr
</
country
>
<
vaue
>
No2
</
value
>
</
text
>
</
second
>
</
first
>
My second xml file is like this:
<
first
>
<
second
>
<
third
>
123
</
third
>
<
text
>
<
type
>
text1
</
type
>
<
country
>
in
</
country
>
<
vaue
>
No5
</
value
>
</
text
>
<
third
>
345
</
third
>
<
text
>
<
type
>
text2
</
type
>
<
country
>
in
</
country
>
<
vaue
>
No3
</
value
>
</
text
>
</
second
>
</
first
>
The output should be like this:
<
first
>
<
second
>
<
third
>
123
</
third
>
<
text
>
<
type
>
text1
</
type
>
<
country
>
fr
</
country
>
<
vaue
>
No1
</
value
>
</
text
>
<
text
>
<
type
>
text1
</
type
>
<
country
>
in
</
country
>
<
vaue
>
No5
</
value
>
</
text
>
<
third
>
345
</
third
>
<
text
>
<
type
>
text2
</
type
>
<
country
>
in
</
country
>
<
vaue
>
No3
</
value
>
</
text
>
<
text
>
<
type
>
text2
</
type
>
<
country
>
in
</
country
>
<
vaue
>
No2
</
value
>
</
text
>
</
second
>
</
first
>
I have tried it in C# like this, but it is not giving me the correct output:
string
[] files =
get
all files
in
a directory;
var masterfile =
new
XDocument();
string
readAllText1 = File.ReadAllText(files[0]);
StreamReader sr =
new
StreamReader(files[0],
true
);
XDocument xdoc1 = XDocument.Load(sr);
XElement newDocument =
new
XElement(xdoc1.Root);
masterfile.Add(newDocument);
foreach
(var file
in
files)
{
if
(file != files[0])
{
StreamReader sr1 =
new
StreamReader(file,
true
);
XDocument xdoc = XDocument.Load(sr1);
masterfile.Root.Add(xdoc.Descendants(
"third"
));
}
}
string
str = Path.GetFileNameWithoutExtension(files[0]).Remove(Path.GetFileNameWithoutExtension(files[0]).Length - 3);
masterfile.Save(textBox2.Text +
"//"
+ str +
".xml"
);
Advance thanks,
Darma
Reply
Answers (
6
)
How to Save labels record to DB
how to download pdf in mvc