Difference behavious between file written by code and by text editor? - c#

I have some xml code that i like to have pretty printed (but is not parsable by tools like XmlDocument etc.) in a browser. I currently write the xml code to a file with
File.WriteAllText(filepath, xmlCode);
When i then open the .xml file in file explorer, I get an error that is can't be parsed. No matter if i open it via code or via file explorer.
However when i copy the exact same message into windows text editor and save it as .xml, it is pretty printed regardless of the browser I open it with. This applies to opening it by code and file explorer.
Does c# or editor add some hidden attributes to the file that is not visible to me (but can be manipulated) which could explain this behaviour?
A colleague of mine said it could have something to do with NTFS streams but I know too little about them.

Thanks for the responses!
It turned out to be more simple than encoding issues and more of a problem of how it was formatted before getting to my end.
Someone must have done:
message.Replace(" ", string.empty);
Which resulted in the xmlns:i part being put together with the attribute name (I belive this is called differently but I don't know the proper name), as such
<AttributeNamexmlns:i="....
My solution:
It still is not parcalable for a XmlDocument or similar for some reason (but that is not necessary for me, as long as it is pretty printed), so my current solution is to open it in a browser (specifically a WebBrowser in Windows Forms, but this should work with a "local" browser too):
First I get rid of the spacing mistake (yes this should be done at an earlier stage in the process, this is just temporary):
var index = msg.IndexOf("xmlns:i");
msg = msg.Insert(index, " ");
Then write it to a file and open it in my custom browser (which is nothing more than a WebBrowser in a form - with nothing modified):
CustomBrowser cb = new CustomBrowser();
cb.Show();
cb.Navigate(filePath);
This then pretty prints the xml doc and displays it. (Thats all I need for my use case)

Related

Trouble opening cshtml file

I have an mvc project while working on it i have opened a cshtml file, then unexpectedly my pc was crashed and restarted, now when i want to open the same cshtml file it opens like the image i have shared
when i open the url in the browser i get this error as bellow image
any help please
I'm not sure what your question is, since you only describe a problem and not specifically what you are trying to do.
But since it's been 20 minutes and nobody is answering, I'll try to help. (I do not have enough reputation to post a comment, or I would have)
The screen in your sample is Visual Studio's binary editor. Your file seems to contain only zeroes, no data. That probably means the file is corrupt. I assume Visual Studio defaults to the binary editor in this case because the file does not contain any text that is possible to display.
I think you'll have to delete the file and restore it from your source control system. (Hopefully you didn't have a lot of work in that file, because it is probably gone.)

Not able to Convert .dwg to .pdf using .Net and True View (AcCtrl)

What I am trying to do is:
I have a Asp Mvc website. In it I need to create a function that does the following:
converts a .dwg (AutoCad) file to a .pdf file
converts a .dwg file to a .dwf file
I started with the, what I tought at the time, easier task to have the .dwg to a .pdf. After some research I found out that a way to do that (without buying a 3rd party license) is to install TrueView on the server and using it (or actually a AcCtrl.dll) to convert the file. Well, I did so. I installed the True View program and added a reference to AcCtrl Component (ACCTRLLib). Then I added a reference to the Dll inside the class file I am working on:
using ACCTRLLib;
So far so good. After that I followed the instructions on this post: PDF conversion using dwg true viewer in VB6
First, this is my code:
public static void ConvertFile()
{
IAcCtrl contrl = new AcCtrl();
contrl.PutSourcePath(#"D:\MMA\Autocat\File1.dwg");
string[] pdfPath = new string[1] { #"D:\MMA\Autocat\File1.dwg" };
contrl.SilentPublish(pdfPath);
}
Then, according to the post, I went to see if there is a registry with the specified path. The path that I have as a registry is the following:
HKEY_CURRENT_USER\Software\Autodesk\DWG TrueView\R13\DWGVIEWR-E001:409\Profiles\<<\Unnamed Profile>>\Dialogs\AcPublishDlg
So, I created a 'String Value' in it with ValueName: Location and ValueData: D:\MMA\Autocat\Testing
Alright. So, thats it. After all that I ran the application and called the function. The debugger goes through the code and everything executes (or at least looks like so) but nothing happens. I don't get a file in the D:\MMA\Autocat\Testing folder. I get no exception, no warning nothing. It just executes and nothing happens.
So what else did I tried. Some of those things might be a little naive or silly to try but nevertheless I did as I happen to be a little desperate.
I tried everything in a console application. I wasnt completely sure that this method is suitable for ASP MVC so I tried the same code with a console application unfortunatelly to the same result.
I added a file name inside the location string value. I changed the Location ValueData from 'D:\MMA\Autocat\Testing' to 'D:\MMA\Autocat\Testing\testFile.pdf' as I noticed that nowhere in the whole process a file name is asked for the converted file.
Following my thought from the previous conclusion I tried to supply the path to the 'result' file to the SilentPublish function.
string[] pdfPath = new string[1] { #"D:\MMA\Autocat\Testing\testFile.pdf" };
contrl.SilentPublish(pdfPath);
Again to no avail. So, my question, or rather questions are:
Is it possible to do it this way and if it is what am I doing wrong?
If its not than can you suggest a way?
Also if a .dwg to .dwf conversion is possible (with the same or different method I am all ears)
Thank you very much
True View does not expose the Autodesk.AutoCAD.PlottingServices namespace. You're going to need either a licensed copy of AutoCAD, RealDWG, or another third party API.
I'm not sure if you are still having this issue, but you should be able to do this with the Autodesk Forge API. Calls can be made from any language with a simple HTTP call. You will have to first convert to SVF and then to DXF and PDF from there.

Opening Word-document (.doc) using C# causes COMException due to File Block Settings

I'm trying to open a really old (binary) Word-file using C#/.NET and Microsoft.Office.Interop.Word. Whenever I try to open this file using the snippet below, it fails with a COMException telling me that "You are attempting to open a file type that is blocked by your File Block settings in the Trust Center."
This also used to happen when opening the same file in MS Word, but after adjusting the settings found in the Trust Center this now works just fine. However, I still cannot open the file using C#. Does anyone know if VS2010 caches these settings, or use its own settings somewhere? I have also tried to create a new project (after fixing the settings in Word) with the same snippet to see if that helped, but it did not.
Word.Application app;
string file = "<filename>";
app = new Word.Application();
try
{
app.Documents.Open(file);
}
catch (COMException e)
{
string s = e.Message;
}
This is implemented with an alternate data stream, supported by the NTFS file system. You access such a stream with the filename:stream syntax. The stream name that stores the info for the file blocking feature is Zone.Identifier:$DATA. It is written by whatever program copied the file, usually a browser.
You can see them with the DIR /R option. The SysInternals' Streams utility permits listing and deleting them. A silly way to get rid of it is copying it to a file system that doesn't support alternate data streams, like a .zip archive or a flash drive and copying it back.
You can look at the content of the stream or edit it with a command like notepad filename:zone.identifier, you'll see this:
[ZoneTransfer]
ZoneId=3
The ZoneId value identifies the origin of the file. Values are -1=unspecified, 0=local machine, 1=intranet, 2=trusted, 3=internet, 4=untrusted. What notepad can do is however not possible in .NET, it explicitly forbids using the : character in file names. The intention is for a human to deal with this, explicitly overriding the file blocking feature. Right-click the file in Explorer, Properties and click the Unblock button. That deletes the stream. Note the Powershell cmdlets mentioned in the first link.

Is it safe to use pdf2swf.exe in C# web application ? (through System.Diagnostics.Process)

I have a question about converting pdf to swf in real time in c# code behind, so that i can use FlexPaper to display the pdf document. I have tried to find any solution to generate the swf, but unluckily, I only got a method which use "pdf2swf.exe" by calling "System.Diagnostics.Process" and just like a call in command prompt in the code behind.
So, is this a proper approach? In order to let code behind to see the exe file, I have to place the exe file within the Web Dir branch, and this file may go to the public.
Can anyone can give me an advice on how to convert pdf to swf in proper way?
thanks
jack
I'd split the process that converts the pdf and shows the created swf.
Let a service of some sorts pick up pdf files in a special directory and convert them to a special directory. This way the asp.net process does not have to be able to start processes and write data. It just has to place the file in the correct folder.
Calling external processes can create dangerous situations if you don't handle error codes correctly. Do you catch convertion erros in any way?

Reading a Bitmap from a TIF file - Works in WinForms not in ASP.NET

EDIT Solution Found: See my post below.
We are writing a library that reads in a TIF file from a scanner. Basically, its a scantron. We are examining the form and reading values from it.
Currently we have a windows test app, and we give it a filepath as a string ("c:\testing\image.tif"). It loads up the tif, reads the document correctly and parses the values.
We also have an ASP.NET web application. We have a test page that does exactly what the windows app does, we hand it an identical string, and i calls the same function on the same class from the same library. It however does NOT read the form correctly. We have verified that it does it fact load up the tif file, and it is actually filled with data (pixels we expect to be white/black are white/black when we examine the Bitmap obect in the immediate window of Visual Studio).
The specific problem is in a library called DataMatrix we use to scan a bar-code off the document. This function is supposed to return a List<string>, each of which is a barcode the library found on the document. In the windows app, this function (DataMatrixDecoder.DecodeBarcode(bitmap)) correctly returns with a Count=1. When using the asp.net app, this returns with Count=0.
Because its the exact same image file, I cannot imagine the problem is in DataMatrix. I can only assume its something with ASP.NET or something.
This isn't even my project, but another guy and I are helping our coworker figure this out, and we are just pulling our hair out. All signs indicate that ASP.NET is correctly loading and handing the image off disk to the "processor" class (which is a class library that uses the DataMatrix stuff, we are not doing ANY code in ASP.NET except for opening/handing the file to the function.).
Does anyone have any ideas as to what it might be, or different things we can check?
I'm not even sure what kind of information to give so I tried to say it all, if you have any questions please ask I'd be more than happy to elaborate on anything. Thanks.
edit:
this is the code on the ascx.cs code-behind, in a button-click event:
if (formReader.ReadTIFF(#"c:\testing\image.tif"))
{
messages.Controls.Add(HtmlHelper.DivSuccess("Read successful."));
}
The formReader class then open the file with a FileStream, and uses that to create a Bitmap. The ASP.NET application is not actually opening the file at all (we were uploading it through a FormUpload control, but after experiencing problems we dummied it down to this). This is the most perplexing thing, that it works in the windows app but not from this web site. ASP.NET has full permissions on that folder to do whatever it wants. It can open the image fine, and the bitmap it creates from the FileStream is the actual image.
edit: Also, the ReadTIFF function right now copies the FileStream into a MemoryStream, ensuring its a not a problem streaming from disk (the entire file is in memory).
How are you passing the filepath to the web application?
It is possible that the function which Decodes might be swallowing some exception.
Use reflector to examine the library (if you have not written it).
I agree. It seems your problem is most probably related to User rights on the directory where you're trying to access the files from. Try giving your Web users the Full access rights on the source directory.
EDIT
Solution Found: The problem was that the open file dialog was changing the CurrentWorkingDirectory. The reason the website never worked, was because the Environment.CurrentDirectory was set incorrectly. When I manually set the CurrentDirectory to the websites' bin folder, parsing works correctly.
Small update. Using the Windows App, and selecting the file via OpenFileDialog, will cause the barcode decoder to fail. Technically, I am using the exact same string to hand to the parser ("c:\testing\image.tif"), yet when I use the OpenFileDialog to get the string, the decoder fails. Is there a clue in this?
update: In fact, even if I don't use the string the OpenFileDialog gives me, if I just open the file dialog at all, it will fail. I don't get this. It's something simple. I need to debug the C++ DataMatrix library, really.

Categories