zad8
This commit is contained in:
parent
095527ea81
commit
fbcfc5edc2
5 changed files with 65146 additions and 0 deletions
65023
IS_Lab2_XMLJson/Assets/data.json
Normal file
65023
IS_Lab2_XMLJson/Assets/data.json
Normal file
File diff suppressed because it is too large
Load diff
20
IS_Lab2_XMLJson/IS_Lab2_XMLJson.csproj
Normal file
20
IS_Lab2_XMLJson/IS_Lab2_XMLJson.csproj
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Assets\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="Assets\data.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
16
IS_Lab2_XMLJson/IS_Lab2_XMLJson.sln
Normal file
16
IS_Lab2_XMLJson/IS_Lab2_XMLJson.sln
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IS_Lab2_XMLJson", "IS_Lab2_XMLJson.csproj", "{77FCD8D4-8635-4E22-8B3E-B8642D35D397}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{77FCD8D4-8635-4E22-8B3E-B8642D35D397}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{77FCD8D4-8635-4E22-8B3E-B8642D35D397}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{77FCD8D4-8635-4E22-8B3E-B8642D35D397}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{77FCD8D4-8635-4E22-8B3E-B8642D35D397}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
27
IS_Lab2_XMLJson/Program.cs
Normal file
27
IS_Lab2_XMLJson/Program.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
using IS_Lab2_XMLJson;
|
||||||
|
|
||||||
|
if (args.Length != 3)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Usage: <mode> <input file> <output file>");
|
||||||
|
Console.WriteLine("Modes: json-to-xml | xml-to-json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mode = args[0];
|
||||||
|
var inputFile = args[1];
|
||||||
|
var outputFile = args[2];
|
||||||
|
|
||||||
|
switch (mode.ToLower())
|
||||||
|
{
|
||||||
|
case "json-to-xml":
|
||||||
|
XmlJsonConverter.JsonToXml(inputFile, outputFile);
|
||||||
|
break;
|
||||||
|
case "xml-to-json":
|
||||||
|
XmlJsonConverter.XmlToJson(inputFile, outputFile);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Console.WriteLine("Invalid mode. Use 'json-to-xml' or 'xml-to-json'.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Conversion complete.");
|
||||||
60
IS_Lab2_XMLJson/XmlJsonConverter.cs
Normal file
60
IS_Lab2_XMLJson/XmlJsonConverter.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
|
namespace IS_Lab2_XMLJson;
|
||||||
|
|
||||||
|
public class XmlJsonConverter
|
||||||
|
{
|
||||||
|
public static string SanitizeTag(string tag)
|
||||||
|
{
|
||||||
|
return Regex.Replace(tag, @"[^a-zA-Z0-9_-]", "_");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void JsonToXml(string jsonFile, string xmlOutput)
|
||||||
|
{
|
||||||
|
var jsonText = File.ReadAllText(jsonFile);
|
||||||
|
var objs = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(jsonText)!;
|
||||||
|
|
||||||
|
var doc = new XmlDocument();
|
||||||
|
var root = doc.CreateElement("Jednostki");
|
||||||
|
doc.AppendChild(root);
|
||||||
|
|
||||||
|
foreach (var obj in objs)
|
||||||
|
{
|
||||||
|
var elem = doc.CreateElement("Jednostka");
|
||||||
|
foreach (var pair in obj)
|
||||||
|
{
|
||||||
|
var pairObject = doc.CreateElement(SanitizeTag(pair.Key));
|
||||||
|
pairObject.InnerText = pair.Value?.ToString() ?? "";
|
||||||
|
|
||||||
|
elem.AppendChild(pairObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
root.AppendChild(elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
File.WriteAllText(xmlOutput, doc.OuterXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void XmlToJson(string xmlFile, string jsonOutput)
|
||||||
|
{
|
||||||
|
var doc = new XmlDocument();
|
||||||
|
doc.Load(xmlFile);
|
||||||
|
|
||||||
|
var root = doc.DocumentElement!;
|
||||||
|
var units = new List<Dictionary<string, object>>();
|
||||||
|
|
||||||
|
foreach (XmlNode unitNode in root.ChildNodes)
|
||||||
|
{
|
||||||
|
var unitDict = new Dictionary<string, object>();
|
||||||
|
foreach (XmlNode childNode in unitNode.ChildNodes)
|
||||||
|
unitDict[childNode.Name] = childNode.InnerText;
|
||||||
|
|
||||||
|
units.Add(unitDict);
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonText = JsonSerializer.Serialize(units, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
File.WriteAllText(jsonOutput, jsonText);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue