Tech
Forums
Jobs
Books
Events
Videos
Conference
Annual Conference
Bcrypt
Ai Conference
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
6
Reply
merge all XML file in a folder in C#
Ask a question
Ask a question
Upload Source Code:
Select only zip and rar file.
darma teja
8y
338
1
Reply
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
Post
Reset
Cancel
Answers (
6
)
Next Recommended Forum
How to Save labels record to DB
how to download pdf in mvc