TestController.cs
8.09 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System;
using System.Linq;
using System.Collections.Generic;
using MapsDb.Models;
namespace Maps.Controllers
{
public class TestController : Controller
{
[HttpGet]
public async Task<IActionResult> Index()
{
Curl curl = new Curl();
string result = await curl.SendRequest();
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(result);
XmlElement xRoot = xDoc.DocumentElement;
if (xRoot.Name != "osm") {
return this.Content(result, "text/xml");
}
int nodes, ways, relations;
nodes = ways = relations = 0;
foreach (XmlNode child in xRoot) {
switch(child.Name) {
case "node":
nodes++;
break;
case "way":
ways++;
break;
case "relation":
relations++;
break;
}
}
string answer = "";
answer += "Nodes count: " + nodes + "\n";
answer += "Ways count: " + ways + "\n";
answer += "Relations count: " + relations;
return this.Content(answer);
}
[HttpGet]
public async Task<IActionResult> Create()
{
string filename = "base.xml";
int trackId = 1;
XDocument doc = new XDocument(
new XElement("library",
new XElement("track",
new XAttribute("id", trackId++),
new XAttribute("genre", "Rap"),
new XAttribute("time", "3:24"),
new XElement("name", "Who We Be RMX (feat. 2Pac)"),
new XElement("artist", "DMX"),
new XElement("album", "The Dogz Mixtape: Who's Next?!")
),
new XElement("track",
new XAttribute("id", trackId++),
new XAttribute("genre", "Rap"),
new XAttribute("time", "5:06"),
new XElement("name", "Angel (ft. Regina Bell)"),
new XElement("artist", "DMX"),
new XElement("album", "...And Then There Was X")
),
new XElement("track",
new XAttribute("id", trackId++),
new XAttribute("genre", "Break Beat"),
new XAttribute("time", "6:16"),
new XElement("name", "Dreaming Your Dreams"),
new XElement("artist", "Hybrid"),
new XElement("album", "Wide Angle")
),
new XElement("track",
new XAttribute("id", trackId++),
new XAttribute("genre", "Break Beat"),
new XAttribute("time", "9:38"),
new XElement("name", "Finished Symphony"),
new XElement("artist", "Hybrid"),
new XElement("album", "Wide Angle")
)
)
);
FileStream stream = new FileStream(filename, FileMode.Append);
doc.Save(stream);
return Content(doc.ToString(), "text/xml");
}
public async Task<IActionResult> Update()
{
string filename = "base.xml";
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
XDocument xDoc = XDocument.Load(stream);
stream.Dispose();
stream = new FileStream(filename, FileMode.Truncate, FileAccess.ReadWrite);
foreach (XElement el in xDoc.Root.Elements("track"))
{
int id = Int32.Parse(el.Attribute("id").Value);
el.SetAttributeValue("id", id + 5);
}
xDoc.Save(stream);
return Content(xDoc.ToString(), "text/xml");
}
public async Task<IActionResult> Add()
{
string filename = "base.xml";
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
XDocument doc = XDocument.Load(stream);
stream.Dispose();
stream = new FileStream(filename, FileMode.Truncate, FileAccess.ReadWrite);
int maxId = doc.Root.Elements("track").Max(t => Int32.Parse(t.Attribute("id").Value));
XElement track = new XElement("track",
new XAttribute("id", ++maxId),
new XAttribute("genre", "Break Beat"),
new XAttribute("time", "5:35"),
new XElement("name", "Higher Than A Skyscraper"),
new XElement("artist", "Hybrid"),
new XElement("album", "Morning Sci-Fi")
);
doc.Root.Add(track);
doc.Save(stream);
return Content(doc.ToString(), "text/xml");
}
public async Task<IActionResult> Delete()
{
string filename = "base.xml";
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
XDocument doc = XDocument.Load(stream);
stream.Dispose();
stream = new FileStream(filename, FileMode.Truncate, FileAccess.ReadWrite);
// IEnumerable<XElement> tracks = doc.Root.Descendants("track").Where(
// t => t.Element("artist").Value == "Hybrid"
// ).ToList();
// foreach (XElement t in tracks)
// {
// t.Remove();
// }
doc.Root.Descendants("track").Where(
t => t.Element("artist").Value == "Hybrid"
).Remove();
doc.Save(stream);
return Content(doc.ToString(), "text/xml");
}
[HttpGet]
public async Task<IActionResult> Relation()
{
Curl curl = new Curl();
string result = await curl.GetRelation(2143112);
XDocument doc = XDocument.Parse(result);
IEnumerable<XElement> relations = doc.Root.Elements("relation");
List<int> relation_ids = new List<int>();
foreach (XElement relation in relations) {
XElement tag = (from t in relation.Elements("tag")
where t.Attribute("k").Value == "ref" select t).SingleOrDefault();
if (tag != null) {
string name = tag.Attribute("v").Value;
if (name.Length == 4) {
relation_ids.Add(Int32.Parse(relation.Attribute("id").Value));
}
}
}
List<int> answer = new List<int>();
foreach (int relation_id in relation_ids) {
string relationXml = await curl.GetRelation(relation_id);
answer.Add(this.SaveRelation(relationXml));
}
// IEnumerable<XElement> tags = from t in doc.Root.Elements("relation").Elements("tag")
// where t.Attribute("k").Value == "ref" select t;
// foreach (XElement tag in tags) {
// string value = tag.Attribute("v").Value;
// System.Console.WriteLine(tag.ToString());
// if(value.Length == 4) {
// answer.Add(value);
// }
// }
// answer.Sort();
return this.Content(string.Join("\n", answer));
}
protected int SaveRelation(string relation)
{
XDocument doc = XDocument.Parse(relation);
XElement tag = (from t in doc.Root.Element("relation").Elements("tag")
where t.Attribute("k").Value == "ref" select t).Single();
int index = Int32.Parse(tag.Attribute("v").Value.Substring(2));
return index;
}
}
}