-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cs
More file actions
44 lines (41 loc) · 1.4 KB
/
Copy pathSource.cs
File metadata and controls
44 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using Newtonsoft.Json;
namespace Moravia.Homework
{
public class Document
{
public string Title { get; set; }
public string Text { get; set; }
}
class Program
{
static void Main(string[] args)
{
var sourceFileName = Path.Combine(Environment.CurrentDirectory, "..\\..\\..\\Source Files\\Document1.xml");
var targetFileName = Path.Combine(Environment.CurrentDirectory, "..\\..\\..\\Target Files\\Document1.json");
try
{
FileStream sourceStream = File.Open(sourceFileName, FileMode.Open);
var reader = new StreamReader(sourceStream);
string input = reader.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
var xdoc = XDocument.Parse(input);
var doc = new Document
{
Title = xdoc.Root.Element("title").Value,
Text = xdoc.Root.Element("text").Value
};
var serializedDoc = JsonConvert.SerializeObject(doc);
var targetStream = File.Open(targetFileName, FileMode.Create, FileAccess.Write);
var sw = new StreamWriter(targetStream);
sw.Write(serializedDoc);
}
}
}