Skip to content

Commit 9d69fc4

Browse files
Added tests
Changed output of console app to json Added ToJsonString to XmlDiffLib
1 parent ee0c7f1 commit 9d69fc4

File tree

10 files changed

+5462
-106
lines changed

10 files changed

+5462
-106
lines changed

XmlDiffLib.Console/Program.cs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Xml;
7+
using System.Xml.XPath;
8+
using System.IO;
9+
using System.Diagnostics;
10+
using Jokedst.GetOpt;
11+
using XmlDiffLib;
12+
13+
namespace XmlDiffLibConsole
14+
{
15+
class Program
16+
{
17+
public enum UsageErrors { InvalidNumberArguments, InvalidArguments }
18+
public static void ShowUsage(UsageErrors error)
19+
{
20+
switch (error)
21+
{
22+
case UsageErrors.InvalidNumberArguments:
23+
Console.WriteLine("ERROR: Invalid number of arguments.");
24+
break;
25+
case UsageErrors.InvalidArguments:
26+
Console.WriteLine("ERROR: Invalid arguments.");
27+
break;
28+
default:
29+
break;
30+
}
31+
Console.WriteLine("USAGE: xmldiff <srcfile.xml> <cmpfile.xml> [-o <filename>] [--csv]");
32+
}
33+
34+
35+
static void Main(string[] args)
36+
{
37+
XmlDiffOptions xDiffOptions = new XmlDiffOptions();
38+
string fromFile = string.Empty;
39+
string toFile = string.Empty;
40+
string outFile = string.Empty;
41+
bool toCsv = false;
42+
43+
var options = new GetOpt("XmlDiff: Tool for finding the difference between two Xml files.",
44+
new[]
45+
{
46+
new CommandLineOption('o', "outfile", "Output file to write to. Files with csv extension open in Excel.",
47+
ParameterType.String, o => outFile = (string)o),
48+
new CommandLineOption('\0', "csv", "Creates a diff csv file and opens in Excel. If no outfile is specified writes output to xmldiff.csv. Default=False",
49+
ParameterType.None, none => toCsv = true),
50+
new CommandLineOption('m', "nomatch", "Don't match text node value types (i.e. 0.00 != 0). Default=False",
51+
ParameterType.None, none => xDiffOptions.MatchValueTypes = false),
52+
new CommandLineOption('\0', "ignoretypes", "If -m or --nomatch is NOT chosen, then this chooses which match types to ignore. " +
53+
"Possible values are (string, integer, double, datetime). Multiple values may be separated by '|'", ParameterType.String,
54+
(types) =>
55+
{
56+
string[] values = ((string)types).Split('|');
57+
foreach (string value in values)
58+
{
59+
switch (value.ToLower().Trim())
60+
{
61+
case "string":
62+
xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlString);
63+
break;
64+
case "integer":
65+
xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlInteger);
66+
break;
67+
case "double":
68+
xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlDouble);
69+
break;
70+
case "datetime":
71+
xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlDateTime);
72+
break;
73+
default:
74+
throw new CommandLineException("Error parsing enumerated values.", "ignoretypes");
75+
}
76+
}
77+
}),
78+
new CommandLineOption('d', "nodetail", "Will not display details of matching nodes. Default=False", ParameterType.None, none => xDiffOptions.MatchDescendants = false),
79+
new CommandLineOption('c', "case", "Case Sensitive. Default=False", ParameterType.None, none => xDiffOptions.IgnoreCase = false),
80+
new CommandLineOption('\0', "2way", "Does a comparison in both directions. Default=False", ParameterType.None, none => xDiffOptions.TwoWayMatch = true),
81+
new CommandLineOption("Required. FromFile", ParameterType.String, file => fromFile = (string)file),
82+
new CommandLineOption("Required. ToFile", ParameterType.String, file => toFile = (string)file)
83+
});
84+
85+
try
86+
{
87+
options.ParseOptions(args);
88+
}
89+
catch (CommandLineException ex)
90+
{
91+
Console.WriteLine("Error: {0}", ex.Message);
92+
return;
93+
}
94+
95+
StreamWriter sw;
96+
XmlDiff xdiff;
97+
try
98+
{
99+
xdiff = new XmlDiff(File.ReadAllText(fromFile), File.ReadAllText(toFile));
100+
xdiff.CompareDocuments(xDiffOptions);
101+
}
102+
catch (Exception ex)
103+
{
104+
Console.WriteLine("Error: {0}", ex.Message);
105+
return;
106+
}
107+
108+
if (toCsv)
109+
{
110+
try
111+
{
112+
string file;
113+
if (!string.IsNullOrEmpty(outFile))
114+
file = outFile;
115+
else
116+
file = "xmldiff.csv";
117+
sw = new StreamWriter(file);
118+
sw.Write((toCsv) ? xdiff.ToCSVString() : xdiff.ToJsonString());
119+
sw.Close();
120+
Process.Start(file);
121+
}
122+
catch (IOException ex)
123+
{
124+
Console.WriteLine("Error: {0}", ex.Message);
125+
return;
126+
}
127+
}
128+
else
129+
{
130+
if (string.IsNullOrEmpty(outFile))
131+
Console.WriteLine(xdiff.ToJsonString());
132+
else
133+
{
134+
try
135+
{
136+
sw = new StreamWriter(outFile);
137+
sw.WriteLine(xdiff.ToJsonString());
138+
sw.Close();
139+
}
140+
catch (IOException ex)
141+
{
142+
Console.WriteLine("Error: {0}", ex.Message);
143+
return;
144+
}
145+
}
146+
}
147+
}
148+
149+
}
150+
151+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("XmlDiffLib.Console")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("ITC")]
12+
[assembly: AssemblyProduct("XmlDiffLib.Console")]
13+
[assembly: AssemblyCopyright("Copyright © ITC 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("3643e190-610a-40da-8d15-2457e64237d6")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3643E190-610A-40DA-8D15-2457E64237D6}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>XmlDiffLib.Console</RootNamespace>
11+
<AssemblyName>XmlDiffLib.Console</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<PropertyGroup>
33+
<StartupObject />
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Jokedst.GetOpt, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
37+
<HintPath>..\XmlDiffLib\packages\Jokedst.GetOpt.1.1.0\lib\Jokedst.GetOpt.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Net.Http" />
47+
<Reference Include="System.Xml" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="Program.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<None Include="packages.config" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ProjectReference Include="..\XmlDiffLib\XmlDiffLib.csproj">
58+
<Project>{50a8aed9-9b85-4c30-a04d-0218e23af00b}</Project>
59+
<Name>XmlDiffLib</Name>
60+
</ProjectReference>
61+
</ItemGroup>
62+
<ItemGroup>
63+
<Content Include="exampleA.xml">
64+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
65+
</Content>
66+
<Content Include="exampleB.xml">
67+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
68+
</Content>
69+
</ItemGroup>
70+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
71+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
72+
Other similar extension points exist, see Microsoft.Common.targets.
73+
<Target Name="BeforeBuild">
74+
</Target>
75+
<Target Name="AfterBuild">
76+
</Target>
77+
-->
78+
</Project>

0 commit comments

Comments
 (0)