When I try to use Compare function from MS API (XmlDiffPatch), it give me an error "illegal characters in path".
This is my code:
public void CompareXMLStructer(string a, string b)
{
try
{
using (var fs = new FileStream(#"C:\Test\result.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var diffWriter = XmlWriter.Create(fs);
var xmlDiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
XmlDiffOptions.IgnoreNamespaces |
XmlDiffOptions.IgnorePrefixes);
var identical = xmlDiff.Compare(a, b, false, diffWriter);
MessageBox.Show(identical ? "YES" : "NO");
diffWriter.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Exception:
Exception thrown: 'System.ArgumentException' in mscorlib.dll
System.ArgumentException: Niedozwolone znaki w ścieżce. //<-That means Illegal expresion in path
w System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
w System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
w System.IO.Path.GetFullPathInternal(String path)
w System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)
w System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri)
w System.Xml.XmlTextReaderImpl..ctor(String url, XmlNameTable nt)
w System.Xml.XmlTextReader..ctor(String url)
w Microsoft.XmlDiffPatch.XmlDiff.OpenDocuments(String sourceFile, String changedFile, XmlReader& sourceReader, XmlReader& changedReader)
w Microsoft.XmlDiffPatch.XmlDiff.Compare(String sourceFile, String changedFile, Boolean bFragments, XmlWriter diffgramWriter)
w WindowsFormsApplication1.Comparision.CompareXMLStructer(String a, String b) w C:\Users\zos-srv\documents\visual studio 2015\Projects\Porownywarka\WindowsFormsApplication1\Comparision.cs:wiersz 44
The thread 0x1334 has exited with code 0 (0x0).
Can it be caused by \r, \n etc. in my string or maybe encoding is wrong?
string:
a "<?xml version=\"1.0\" encoding=\"Windows-1250\"?>\r\n<Dokument idDokumentu=\"6183457\" numer=\"32178.2015\" idSprawy=\"6187041\" wersja=\"1\" dataDokumentu=\"2015-09-09T00:00:00\" numerObcy=\"\" rodzajObcy=\"\" idObcy=\"\" vidObcy=\"\" wersjonowanie=\"0\" statusBIP=\"E\" korespSystemWew=\"N\" nrWgRejestru=\"\" system=\"EDOKUMENT\" oznWysylka=\"true\" wysylka=\"false\" odbiorOsobisty=\"false\" kopia=\"false\" kodKreskowy=\"\" dostep=\"1\" format=\"\" typ_dublin_core_metadata=\"8\" rwa=\"0003\" znakDokumentu=\"\" obcy=\"false\" odwzorowanie=\"\" znak_sprawy_alt=\"\" kat_arch=\"A\" idDokumentuPierw=\"0\" numerWSprawie=\"4\" czySaUwagi=\"false\" ntas=\"false\" idSkladuChronologicznego=\"0\" zatwierdzonyUzytk=\"true\">\r\n <Opis>\r\n <![CDATA[Testowy dokument]]>\r\n </Opis>\r\n <Notatka>\r\n <![CDATA[]]>\r\n </Notatka>\r\n <Typ idTypu=\"12938\" nazwa=\"OR Inne\" kategoriaBIP=\"\" sciezkaBIP=\"\" podlegaZatwierdzaniu=\"true\" czyBip=\"false\"/>\r\n <Status idStatusu=\"0\" nazwa=\"\"/>\r\n <Podmiot idPodmiotu=\"6183458\" typPodmiotu=\"G\" idPodmiotuExt=\"5573\" rodzajPodmiotu=\"K\" imie=\"\" nazwisko=\"\" nazwaFirmy=\"Rekord Systemy Informatyczne Sp.z o.o.\" nazwaSkroconaFirmy=\"REKORD\" PESEL=\"\" NIP=\"\" REGON=\"\" idZewnetrzne=\"\" rodzajPodmiotuExt=\"F\" email=\"\">\r\n <Adres kodPocztowy=\"43-300\" miasto=\"Bielsko-Biała\" ulica=\"Kasprowicza\" numerDomu=\"5\" numerLokalu=\"\" kraj=\"Polska\" adresSkrytki=\"\" dzielnica=\"\" skrytkaPocztowa=\"\" adresZagraniczny=\"false\"/>\r\n </Podmiot>\r\n <Folder idFolderu=\"4823\" nazwa=\"MIROSŁAW ZIAJA\"/>\r\n <Utworzenie symbolPracownika=\"216\" nazwisko=\"ZIAJA\" imie=\"MIROSŁAW\" data=\"2015-09-09T11:48:36\"/>\r\n <Dysponenci>\r\n <Dysponent ident=\"6183461\" symbolPrac=\"216\" nazwisko=\"MIROSŁAW ZIAJA\" imie=\"\" wiodacy=\"true\" tylko.do.odczytu=\"false\" obcyId=\"\" obcyLogin=\"\" stanowisko=\"Inspektor (OR-o)\" rodzaj=\"P\"/>\r\n </Dysponenci>\r\n <Zatwierdzenia>\r\n <Zatwierdzenie data=\"2015-09-14T14:24:44\" rodzaj=\"1\" czyBlokuje=\"true\" kierownicze=\"false\">\r\n <Pracownik symbolPracownika=\"216\" nazwisko=\"ZIAJA\" imie=\"MIROSŁAW\" stanowisko=\"\" stanowiskoIdent=\"0\" pracIdent=\"216\" funkcjaIdent=\"0\"/>\r\n </Zatwierdzenie>\r\n </Zatwierdzenia>\r\n</Dokument>\r\n"
As you can read in the stack trace, the first two strings of xmlDiff.Compare(string, string, ...) accept paths, not XML strings.
I don't know where the documentation for this library resides, so I don't know whether there are Stream overloads, for example. If there aren't, just temporarily write the files to disk.
Related
Have been playing around with machine learning as of late, made a basic machine learning algorithm. It was working perfectly and then I broke something and now it refuses to save the model.
here's the code:
private static string MODEL_FILEPATH = #"MLModel.zip";
// Create MLContext to be shared across the model creation workflow objects
// Set a random seed for repeatable/deterministic results across multiple trainings.
private static MLContext mlContext = new MLContext(seed: 1);
public static void CreateModel()
{
// Load Data
IDataView trainingDataView = mlContext.Data.LoadFromTextFile<ModelInput>(
path: TRAIN_DATA_FILEPATH,
hasHeader: true,
separatorChar: ',',
allowQuoting: true,
allowSparse: false);
// Build training pipeline
IEstimator<ITransformer> trainingPipeline = BuildTrainingPipeline(mlContext);
// Evaluate quality of Model
//Evaluate(mlContext, trainingDataView, trainingPipeline);
// Train Model
ITransformer mlModel = TrainModel(mlContext, trainingDataView, trainingPipeline);
// Save model
SaveModel(mlContext, mlModel, MODEL_FILEPATH, trainingDataView.Schema);
}
private static void SaveModel(MLContext mlContext, ITransformer mlModel, string
modelRelativePath, DataViewSchema modelInputSchema)
{
try
{
mlContext.Model.Save(mlModel, null, (GetAbsolutePath(modelRelativePath)));
}
catch (Exception e)
{
Console.WriteLine(e.ToString() + "\n" + e.Message + ": " + GetAbsolutePath(modelRelativePath));
}
}
this code was automatically generated, I just removed the comment, added the try catch and specified the model path.
here's the exception:
> Exception thrown: 'System.ArgumentException' in mscorlib.dll
System.ArgumentException: The path is not of a legal form.
at System.IO.Path.NewNormalizePath(String path, Int32 maxPathLength, Boolean expandShortPaths)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean
expandShortPaths)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.Path.GetFullPath(String path)
at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
at Microsoft.ML.RepositoryWriter.CreateNew(Stream stream, IExceptionContext ectx, Boolean
useFileSystem)
at Microsoft.ML.ModelOperationsCatalog.Save(ITransformer model, DataViewSchema inputSchema, Stream
stream)
at Microsoft.ML.ModelOperationsCatalog.Save(ITransformer model, DataViewSchema inputSchema, String
filePath)
at MachineLearningTest.ModelBuilder.SaveModel(MLContext mlContext, ITransformer mlModel, String
modelRelativePath, DataViewSchema modelInputSchema) in
C:\Users\Michael\Source\Repos\new\MachineLearingTest\ModelBuilder.cs:line 83
The path is not of a legal form.:
C:\Users\Michael\Source\Repos\new\MachineLearingTest\bin\x64\Debug\MLModel.zip
and this is the file which is supposed to contain the model(it is empty)
Below are the Stacktrace;
System.NotSupportedException
HResult=0x80131515
Message=The given path's format is not supported.
Source=mscorlib
StackTrace:
at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at EntryLog.Handlers.StreamEntryLogs.StreamWritter(String log, String foldername) in C:\Users\JNyingi\source\repos\EntryLog\EntryLog\Handlers\StreamEntryLogs.cs:line 31
at EntryLog.EntryLog.LogWarning(String Warning) in C:\Users\JNyingi\source\repos\EntryLog\EntryLog\EntryLog.cs:line 55
at EntryLogConsoleTest.Program.Main(String[] args) in C:\Users\JNyingi\source\repos\EntryLogConsoleTest\EntryLogConsoleTest\Program.cs:line 21
This exception was originally thrown at this call stack:
System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(string)
System.IO.FileStream.Init(string, System.IO.FileMode, System.IO.FileAccess, int, bool, System.IO.FileShare, int, System.IO.FileOptions, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES, string, bool, bool, bool)
System.IO.FileStream.FileStream(string, System.IO.FileMode, System.IO.FileAccess)
EntryLog.Handlers.StreamEntryLogs.StreamWritter(string, string) in StreamEntryLogs.cs
EntryLog.EntryLog.LogWarning(string) in EntryLog.cs
EntryLogConsoleTest.Program.Main(string[]) in Program.cs
The exception is coming about from the following lines;
string filePath = System.IO.Path.Combine(EntryLog.LogPath.AbsolutePath, currentTimeFilename + " - " + $"{foldername}.log");
var fileStreamer = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
var streamWriter = new StreamWriter(fileStreamer);
The LogPath is obtained by this method;
LogPath = new Uri(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
I have tried all manner of debugging but it always throws the above exception at StreamWriter. Kindly assist me in resolving this. I'm using 4.5.2 .net Framework
FILE PATH
The file path in question is this;
C:\Users\JNyingi\source\repos\EntryLogConsoleTest\EntryLogConsoleTest\bin\Debug
CURRENT TIME AND FOLDER NAME
string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
string foldername = "Log"
the problem is the : in your filename
string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
^
Change it to - or _ or even a . for example and the error disappears
string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH_mm");
Using ILSpy you can find that the code of the method EmulateFileIOPermissionChecks (which raises the NotSupportedException) is:
internal static void EmulateFileIOPermissionChecks(string fullPath)
{
if (AppContextSwitches.UseLegacyPathHandling || !PathInternal.IsDevice(fullPath))
{
if (PathInternal.HasWildCardCharacters(fullPath))
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPathChars"));
}
if (PathInternal.HasInvalidVolumeSeparator(fullPath))
{
throw new NotSupportedException(Environment.GetResourceString("Argument_PathFormatNotSupported"));
}
}
}
So your path contains invalid chars.
EDIT
If in your settings hours - minutes separator is a colon (see your datetime formatted string), please consider that ':' cannot be used in a path, but after driver letter.
Today i run Visual Studio and open my project, that i not opened about 5 months.
And running test (it was be OK) invoke IOException with additional information: "Bad descriptor".
It was in this line:
var defaultConsoleEncoding = Console.InputEncoding;
Console.InputEncoding = TryGetEncoding("UTF-8"); // <--- There is error
...
public static Encoding TryGetEncoding(string encoding)
{
try
{
return Encoding.GetEncoding(encoding);
}
catch (ArgumentException)
{
Logger.WarnAndPrint($"Can't using {encoding} encoding. Fallback to utf-8");
return Encoding.UTF8;
}
}
All my tests were OK, but now all fails with that error. I never chage code in project. And i try many encodings - nothing work.
What's the problem?
P.S. This is stack trace
System.IO.IOException was unhandled by user code
HResult=-2147024890
Message=Неверный дескриптор.
Source=mscorlib
StackTrace:
в System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
в System.Console.set_InputEncoding(Encoding value)
в PasswordListGenerator.Substitutions.Substitution.Process() в C:\GITHUB\passwordlistgenerator\PasswordListGenerator\PasswordListGenerator\Substitutions\Substitution.cs:строка 89
в PasswordListGeneratorTest.SubstitutionTests.SkipManySymbols_ShouldReturnSubstitutions() в C:\GITHUB\passwordlistgenerator\PasswordListGenerator\PasswordListGeneratorTest\SubstitutionTests.cs:строка 588
InnerException:
This is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using unfreez_wrapper;
using Shell32;
namespace DownloadImages
{
public partial class Form1 : Form
{
string f;
string UrlsPath;
int counter;
UnFreezWrapper uf;
string localFilename;
public Form1()
{
InitializeComponent();
uf = new UnFreezWrapper();
counter = 0;
localFilename = #"d:\localpath\";
UrlsPath = #"d:\localpath\Urls\";
using (WebClient client = new WebClient())
{
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=1&continent=europa#",localFilename + "test.html");
client.DownloadFile("http://www.sat24.com/en/eu?ir=true", localFilename + "test1.html");
}
f = File.ReadAllText(localFilename + "test1.html");
test("image2.ashx", "ir=true");
}
private void test(string firstTag, string lastTag)
{
List<string> imagesUrls = new List<string>();
int startIndex = 0;
int endIndex = 0;
int position = 0;
string startTag = firstTag;//"http://www.niederschlagsradar.de/images.aspx";
string endTag = lastTag;//"cultuur=en-GB&continent=europa";
startIndex = f.IndexOf(startTag);
while (startIndex > 0)
{
endIndex = f.IndexOf(endTag,startIndex);
if (endIndex == -1)
{
break;
}
string t = f.Substring(startIndex, endIndex - startIndex + endTag.Length);
imagesUrls.Add(t);
position = endIndex + endTag.Length;
startIndex = f.IndexOf(startTag,position);
}
string item = imagesUrls[imagesUrls.Count - 1];
imagesUrls.Remove(item);
for (int i = 0; i < imagesUrls.Count; i++)
{
using (WebClient client = new WebClient())
{
client.DownloadFile(imagesUrls[i], UrlsPath + "Image" + counter.ToString("D6"));
}
counter++;
}
List<string> files = Directory.GetFiles(UrlsPath).ToList();
uf.MakeGIF(files, localFilename + "weather", 80, true);
}
First im downloading this html as html file:
http://www.sat24.com/en/eu?ir=true
There there is animation of 9 different images/gifs.
I want to download each gif url. So on the hard disk i will get 9 gifs.
When reading the file http://www.sat24.com/en/eu?ir=true in the content inside i see:
var imageUrls = ["/image2.ashx?region=eu&time=201309162345&ir=true","/image2.ashx?region=eu&time=201309162330&ir=true","/image2.ashx?region=eu&time=201309162315&ir=true","/image2.ashx?region=eu&time=201309162300&ir=true","/image2.ashx?region=eu&time=201309162245&ir=true","/image2.ashx?region=eu&time=201309162230&ir=true","/image2.ashx?region=eu&time=201309162215&ir=true","/image2.ashx?region=eu&time=201309162200&ir=true","/image2.ashx?region=eu&time=201309162145&ir=true"];
And inside the List: imagesUrls i see this 9 urls:
For example this is in index 0 : image2.ashx?region=eu&time=201309162345&ir=true
I tried without the image2.ashx?
But in both cases im getting an error on the line:
client.DownloadFile(imagesUrls[i], UrlsPath + "Image" + counter.ToString("D6"));
ArgumentException
Illegal characters in path
Before this when i used the test.html and the two other tags start and end it was working without any problem.
But now im using test1.html and this two tags: test("image2.ashx", "ir=true");
But getting the exception.
When i took one image url for example : image2.ashx?region=eu&time=201309170015&ir=true
And tried to surf to it in chrome im getting no rsults it tried to search for it in google .
Its not even a url .
This is the full exception error:
System.ArgumentException was unhandled
HResult=-2147024809
Message=Illegal characters in path.
Source=mscorlib
StackTrace:
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.Security.Permissions.FileIOPermission.CheckIllegalCharacters(String[] str)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Path.GetFullPath(String path)
at System.Net.WebClient.GetUri(String path)
at System.Net.WebClient.DownloadFile(String address, String fileName)
at DownloadImages.Form1.test(String firstTag, String lastTag) in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Form1.cs:line 79
at DownloadImages.Form1..ctor() in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Form1.cs:line 45
at DownloadImages.Program.Main() in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Then how can i download the images one by one from this url ? http://www.sat24.com/en/eu?ir=true
When i used the test.html and the startTag ""http://www.niederschlagsradar.de/images.aspx": and endTag: ""cultuur=en-GB&continent=europa""
It worked perfect !
But now with test1.html and the two different tags its not working.
At the point of the exception, what precisely is in imagesUrls[i] ?
Are you saying that it is something like image2.ashx?region=eu&time=201309162345&ir=true
If so, you need to prepend the protocol and server to it, i.e. prepend http://www.sat24.com/ to give a URI of http://www.sat24.com/image2.ashx?region=eu&time=201309162345&ir=true
However, another problem is, you are searching for image2.ashx for a start tag and then ir=true as an end tag. Looking at the source of that page, there are numerous image2.ashx URIs which do not end with ir=true.
e.g. http://www.sat24.com/image2.ashx?button=af260x160
When you find the start tag in that URI, you're going to get an enormous mass of HTML before you find the end tag.
I have a solution with several projects, a main project, a globalization project and a test project.
When code in the main project retreives a message from the Messages.de.resx file of the globalization project everything works fine.
But when I copy the same code to the test project, I get a MissingManifestResourceException telling me no resources were found for the specified or neutral culture:
System.Resources.MissingManifestResourceException ist aufgetreten.
Message=Für die angegebene Kultur oder die neutrale Kultur konnten
keine Ressourcen gefunden werden. Stellen Sie sicher, dass
EGR_IQone_Globalization.Messages.resources beim Kompilieren richtig in
die Assembly EGR_IQone_Globalization eingebettet wurde, oder dass die
erforderlichen Satellitenassemblys geladen werden können und
vollständig signiert sind. Source=mscorlib StackTrace:
bei System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String
fileName)
bei System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo
culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean
createIfNotExists, StackCrawlMark& stackMark)
bei System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo
requestedCulture, Boolean createIfNotExists, Boolean tryParents,
StackCrawlMark& stackMark)
bei System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo
culture, Boolean createIfNotExists, Boolean tryParents)
bei System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
bei System.Resources.ResourceManager.GetString(String name)
bei EGR_IQone_Globalization.UserMessage.GetMessage(String msgID, String[] arguments) in
C:\projects\EGR_IQoneH\EGR_IQone\EGR_IQone_Globalization\UserMessage.cs:Zeile
28. InnerException:
Normally, the code works just using the .resx file and even using resgen to compile it into a .resources file changes nothing.
I thought it might have to do with the ResourceManager or the specified Assembly, but I could not see any difference between the call from the main project and the call from the test project.
This is the code:
public static class UserMessage
{
private static ResourceManager _resourceManager;
static UserMessage()
{
string baseName = Assembly.GetAssembly(typeof(UserMessage)).GetName().Name + ".Messages.de";
Console.WriteLine(baseName);
_resourceManager = new ResourceManager(baseName, Properties.GlobalizationAssembly);
}
public static string GetMessage(string msgID, params string[] arguments)
{
string msg = "";
string error = "[Message Error] cannot read Message " + msgID;
try
{
//DefaultLanguage = 'de'
//using the GetString overload with or without CultureInfo paramter makes no difference
msg = _resourceManager.GetString(msgID, new CultureInfo(Properties.DefaultLanguage));
for (int i = 0; i < arguments.Length; i++)
{
msg = msg.Replace("{" + i.ToString() + "}", arguments[i]);
}
}
catch (Exception ex)
{
Console.WriteLine(error + "\r\n" + ex.ToString());
return error;
}
return msg;
}
}
http://pastebin.com/L0YNxyfK
Thanks!
I've had the same error - it suddenly occurred even though the application had been running for a while.
It helped to set the Thread.CurrentThread.CurrentUICulture before getting the resource.
Try the following or something similar:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
msg = _resourceManager.GetString(msgID);