how to get the current item being written by an XMLWriter - c#

I have a series of settings being written out by an XMLWriter in C#. Here's some code:
try
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
using (writer = XmlWriter.Create("PCOB2NET.XML", settings))
{
// Write XML data.
writer.WriteStartElement("PowerCOBOL2NETMigration");
writer.WriteStartElement("config");
writer.WriteElementString("VSVersion", selectedVSver);
writer.WriteElementString("path2SelectedVSVerProjects", path2SelectedVSVerProjects);
if (path2VSoverridden)
writer.WriteElementString("path2VSoverridden", "true");
else
writer.WriteElementString("path2VSoverridden", "false");
writer.WriteElementString("path2PRCfile", path2PRCfile);
writer.WriteElementString("path2XMLfile", path2XMLfile);
writer.WriteElementString("path2VSProject", path2VSProject);
... and so on.
My problem is that if there is an Exception (like a null field, for instance) it goes to the catch block and reports the exception as we would expect, but I don't know WHICH field it was writing at the time.
My question:
Is there any way I can get the current string being written, when an Exception occurs? I searched the web without success and I looked through every property and method of XMLWriter but I can't find a way to do it. Is there maybe a certain type of Exception trap that will give it to me? Any help or thoughts appreciated.

Try xml linq which is a new Net Library for reading and writing xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string selectedVSver = "";
string path2SelectedVSVerProjects = "";
Boolean path2VSoverridden = true;
string path2PRCfile = "";
string path2XMLfile = "";
string path2VSProject = "";
XDocument doc = new XDocument();
XElement powerCOBOL2NETMigration = new XElement("PowerCOBOL2NETMigration", new object[] {
new XElement("config", new object[] {
new XElement("VSVersion", selectedVSver),
new XElement("path2SelectedVSVerProjects", path2SelectedVSVerProjects),
new XElement("path2VSoverridden", path2VSoverridden),
new XElement("path2PRCfile", path2PRCfile),
new XElement("path2XMLfile", path2XMLfile),
new XElement("path2VSProject", path2VSProject)
})
});
doc.Add(powerCOBOL2NETMigration);
doc.Save(FILENAME);
}
}
}

The simplest approach is to wrap small helper methods around XmlWriter.WriteStartElement etc. and catch your exceptions there.

It turns out I was looking for a solution at the wrong end of the problem. Instead of puzzling over XMLWriter, what I SHOULD have done was investigate the EXCEPTION.
(I did do this but only for the Exception Class, I needed to look further.) The problem looked like it could be easily solved by nesting some exception catch blocks, so:
catch (ArgumentNullException anEx)
{
// retrieve the offending field name from anEX...
badField = anEx.ParamName;
...
}
catch (ArgumentException aEx)
{
// retrieve the offending field name from aEX...
badField = aEx.ParamName;
...
}
catch (Exception ex)
{
...
However, I was misled by the "ParamName" which does not mean what I thought it did... This always returned null and I was no better off.
After spending a number of hours reading everything I could about Exception handling in C#, I am much better informed, but no wiser. I can't find a solution with the Framework so I have little recourse but to push all the settings through a method which will do the actual write and trap any exception at that moment, with the offending field available as it will have been passed into that method.
I think this inelegant, but I can't spend more time on it. Many thanks to all who responded and those who thought about responding... :-) I'll leave this open in case somebody really has a solution, for a few days.

Related

Revit IExternal Application Error- doesn't seems to run at all

Should we have something with the External application to properly register the event?
I also tried putting two breakpoints one inside the start module and other inside the Export module.
the first responded and waited for me to continue and the next didn't respond(hope did not run the line)
Also,I had manually tried coping the addin file to the addin location to avoid any post build event error but still doesnt seem to work.
could you tell me what I am I doing wrong here.
Here is the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.DB.Events;
using System.IO;
namespace UserDataSheet
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class UserDataSheetclass : IExternalApplication
{
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
try
{
// Register event.
application.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
return Result.Succeeded;
}
catch (Exception)
{
return Result.Failed;
}
}
public void ExportLog(object sender, DocumentOpenedEventArgs args)
{
var doc = sender as Document;
var isFamilyDoc = doc.IsFamilyDocument;
// variables to use
string RevitUserName = "";
DateTime OpenTime = new DateTime();
string localUserName = "";
string filename = "";
string filepath = "";
string content = "";
if (isFamilyDoc == false)
{
var IsloggedIn = Autodesk.Revit.ApplicationServices.Application.IsLoggedIn;
if (IsloggedIn == true )//&& doc.IsModelInCloud == true)
{
// use variables
filepath = doc.PathName;
filename = doc.Title;
RevitUserName = doc.Application.Username;
OpenTime = DateTime.Now;
localUserName = System.Environment.MachineName;
}
content = string.Format("Date and time : {0} \n Revit UserName : {1} \n Local PC UserName : {2} \n FileName : {3} \n FilePath : {4} "
, OpenTime.ToString(), RevitUserName, localUserName, filename, filepath);
TaskDialog.Show("Model Open Writer info", "user and file details : \n " + content);
}
var writefilepath = Path.GetTempPath();
var Writefile = writefilepath + "//records.txt";
FileStream fs = new FileStream(Writefile, FileMode.Append);
StreamWriter writer = new StreamWriter(fs);
writer.Write(content);
writer.Close();
File.OpenRead(Writefile);
}
}
}
First of all, you can completely remove the TransactionMode and RegenerationOption. The latter is completely obsolete and does nothing at all anywhere whatsoever. The former is only useful when declaring an external command. It is useless and ignored in the context of an external application.
Secondly, to address your question: you can set a breakpoint in the beginning of OnStartup. If the breakpoint is not hit, your add-in is not being loaded at all. Probably something is wrong with your add-in registration, e.g., in the add-in manifest *.addin file.
Go back to square one, i.e., work through the getting started material and the developer guide instructions on registering and loading a Revit add-in.
If the breakpoint in OnStartup is hit, then your add-in is loading correctly, which means that the add-in manifest *addin file is OK. So, you do not need to worry about that. The VisibilityMode tag is not used for external applications, by the way.
Thanks, Jeremy It worked
Firstly I apologies for adding this as answer( I don't know how to add codes in comment)
It worked when I deleted my Addin file and recreated it may be I had made some mistake in it.
meanwhile I have copied the following code from examples and used it,honestly I did't understand this line of the code.
"public void ExportLog(object sender, DocumentOpenedEventArgs args)"
can you point to a right source that explains this part. I have three questions here :
what object type is sender and args are they of type application?
How do I add a 3rd parameter to this method say I want the user to input a string to name the file the data is copied to.
Can I do this
var newEvent = new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
instead of
application.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
why does all example use += is this to register the event every time a new instance of Revit is opened?
Thanks for your help.
You can see the class of sender yourself by setting a breakpoint at the beginning of ExportLog and looking in the debugger.
No, you cannot modify the signature of the event handler. It is predetermined by the Revit API.
Yes.
It sounds to me as if you might save some time and effort for yourself by learning a bit more about the basics of C# and .NET programming in general before continuing to tackle this task.

Problems to Get Data using Tesers on nHapi hl7

Hello again dear community, i am using " Tersers " to get data from an hl7 v2 messeage, its a ADT_A08 messeage.
Well the thing is that, im trying to get the PatientName with this: string test = terser.Get("PID-5-1"); and that return me nothing, instead if i use the same but changing the command for something like "MSH-16-2" i dont remember, but its work, why it still happend me that? i need to use things like PID-5-2 or EVN-2 , that commands does not work at all. The same happens when I use "MSH-152-2"; the return is the name of a medication, but that is typical of one of my hl7-v2 messages, but I would have to use "RXE-2-1" to get the same data. I apologize for my english , im spanish-language native.
using NHapi.Base.Model;
using NHapi.Base.Util;
using NHapi.Base.Parser;
using System.Diagnostics;
using NHapi.Model.V23.Message;
using System.IO;
public void someMethod()
{
string msg = txtHL7m.Text;
PipeParser pParser = new PipeParser();
var iMesseage = pParser.Parse(msg, "2.3");
try
{
var terser = new Terser(iMesseage);
string test = terser.Get(txtTerserExpression.Text);
txtTerserResults.Text = test;
}
catch
{
Console.WriteLine("ERROR");
}
}
//I expect the patientsData using the correct sintaxis of tesers

How to use QuickFix to read secdef.dat file

I'm trying to read the static securities definition file from the CME, located at:
ftp://ftp.cmegroup.com/fix/Production/secdef.dat.gz
Since they seem to be standard fix messages, I thought I could use QuickFix to help me read them into C# rather than parsing the file myself. I created a test app that basically does what I want, but I'm having 2 issues:
1) I'm getting a QuickFix exception "Invalid message: Header fields out of order" when forming the message from the string. If I set the "validate" boolean to false, this message disappears and the constructor succeeds, but may be an indicator for the next issue.
2) Upon calling p.Crack, I'm getting the QuickFix exception "QuickFix.UnsupportedMessageType", but there doesn't seem to be any indication of what the message type is that is supposedly unsupported.
Anyway, maybe QuickFix wasn't intended to be used in this way, but any ideas on how to get this to work?
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using QuickFix;
namespace TestQuickFix
{
class Program : QuickFix.MessageCracker
{
static void Main(string[] args)
{
int count = 0;
string line;
Program p = new Program();
StreamReader file = new StreamReader(#"C:\secdef.dat");
while (((line = file.ReadLine()) != null && count < 10))
{
// ISSUE #1 REQUIRES false 2ND ARG WHEN CREATING THE MESSAGE
Message m = new Message(line, false);
// ISSUE #2 Exception of type 'QuickFix.UnsupportedMessageType' was thrown.
p.Crack(m, new SessionID("beginString", "senderCompID", "targetCompID"));
}
file.Close();
}
public void OnMessage(QuickFix.FIX50.SecurityDefinition secDef, SessionID sessionID)
{
Console.WriteLine(secDef.ToString());
}
}
}
The messages seems to be in FIX50sp2 format, supported by QuickFIX. (Please take a look at the tag 1128=9).
http://www.onixs.biz/fix-dictionary/5.0.SP2/tagNum_1128.html
BUT every single message seems to be not-well formatted. In the header are missed tag 8 (should be the BeginString), and also the tag 56 (TargetCompID), that are mandatory.
Therefore in order to load a single line in a message you must put the "false" parameter to avoid validation.
I suppose the second error is related to the not-well formatted messages.
After emailing the QuickFix listserv with this question, I was able to get enough information to get this to work. Although each line still seems to be malformed for some reason, if I keep validation off, I can get the parser to do exactly what I need it to with the following simplified code:
using System;
using System.IO;
using QuickFix;
using QuickFix.DataDictionary;
namespace TestQuickFix
{
class Program
{
private const int MAX_LINES = 10;
static void Main(string[] args)
{
DataDictionary dd = new QuickFix.DataDictionary.DataDictionary("fix\\FIX50SP2.xml");
StreamReader file = new StreamReader(#"C:\secdef.dat");
int count = 0; string line;
while (((line = file.ReadLine()) != null && count++ < MAX_LINES))
{
QuickFix.FIX50.SecurityDefinition secDef = new QuickFix.FIX50.SecurityDefinition();
secDef.FromString(line, false, dd, dd);
Console.WriteLine(secDef.SecurityDesc);
}
file.Close();
}
}
}

How do I re-assign the standard error stream such that exceptions thrown by library code also get written to file instead of the console?

In the following test program, the error output is correctly written to Error.txt.
using System;
using System.IO;
public class Test{
public static void Main(string[] args){
Console.SetOut(new StreamWriter("Output.txt", true));
Console.SetError(new StreamWriter("Error.txt", true));
int[] test = new int[1];
Console.Error.WriteLine(test[0]);
}
}
However, if we change the line
Console.Error.WriteLine(test[0]);
to
Console.Error.WriteLine(test[7]);
which will cause an exception, the error message for this exception gets printed to the console instead of to the file. How can I programmatically set it up so that the error message for system-thrown exceptions is also redirected to a file?
Simple console redirection (2> or >) is not an option because of the context in which this program is run.
I don't think the Console.SetError method actually changes the "DOS" error output of the application, just where Console.Error outputs. You should try PInvoking SetStdHandle. For example of this, see here: Redirect stdout+stderr on a C# Windows service
I believe, error output doesn't work the way you try using it. It is just a way to output information about some errors. Look at this http://msdn.microsoft.com/en-us/library/system.console.seterror.aspx
If you want to write something about an error, you need to do the next:
Console.Error.WriteLine("Error Log for Application {0}", appName);
so if you want to write info about exceptions to stderr, you need to catch your exceptions and direct its messages to stderr.
...
catch(Exception e) { Console.Error.WriteLine(e.Message); }
Are you looking for something like this?
FileStream errorFileStream = new FileStream("Error.txt", FileMode.OpenOrCreate);
StreamWriter errorStreamWriter = new StreamWriter(errorFileStream);
Console.SetError(errorStreamWriter);
string[] test = new string[2];
test[0] = "Hello";
test[1] = "World";
try
{
Console.WriteLine(test[2]);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
Console.Error.Close();

How do I use XmlSerializer to handle different namespace versions?

I am using the .NET XmlSerializer class to deserialize GPX files.
There are two versions of the GPX standard:
<gpx xmlns="http://www.topografix.com/GPX/1/0"> ... </gpx>
<gpx xmlns="http://www.topografix.com/GPX/1/1"> ... </gpx>
Also, some GPX files do not specify a default namespace:
<gpx> ... </gpx>
My code needs to handle all three cases, but I can't work out how to get XmlSerializer to do it.
I am sure there must be a simple solution because this a common scenario, for example KML has the same issue.
I have done something similar to this a few times before, and this might be of use to you if you only have to deal with a small number of namespaces and you know them all beforehand. Create a simple inheritance hierarchy of classes, and add attributes to the different classes for the different namespaces. See the following code sample. If you run this program it gives the output:
Deserialized, type=XmlSerializerExample.GpxV1, data=1
Deserialized, type=XmlSerializerExample.GpxV2, data=2
Deserialized, type=XmlSerializerExample.Gpx, data=3
Here is the code:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
[XmlRoot("gpx")]
public class Gpx {
[XmlElement("data")] public int Data;
}
[XmlRoot("gpx", Namespace = "http://www.topografix.com/GPX/1/0")]
public class GpxV1 : Gpx {}
[XmlRoot("gpx", Namespace = "http://www.topografix.com/GPX/1/1")]
public class GpxV2 : Gpx {}
internal class Program {
private static void Main() {
var xmlExamples = new[] {
"<gpx xmlns='http://www.topografix.com/GPX/1/0'><data>1</data></gpx>",
"<gpx xmlns='http://www.topografix.com/GPX/1/1'><data>2</data></gpx>",
"<gpx><data>3</data></gpx>",
};
var serializers = new[] {
new XmlSerializer(typeof (Gpx)),
new XmlSerializer(typeof (GpxV1)),
new XmlSerializer(typeof (GpxV2)),
};
foreach (var xml in xmlExamples) {
var textReader = new StringReader(xml);
var xmlReader = XmlReader.Create(textReader);
foreach (var serializer in serializers) {
if (serializer.CanDeserialize(xmlReader)) {
var gpx = (Gpx)serializer.Deserialize(xmlReader);
Console.WriteLine("Deserialized, type={0}, data={1}", gpx.GetType(), gpx.Data);
}
}
}
}
}
Here's the solution I came up with before the other suggestions came through:
var settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
using (var reader = XmlReader.Create(filePath, settings))
{
if (reader.IsStartElement("gpx"))
{
string defaultNamespace = reader["xmlns"];
XmlSerializer serializer = new XmlSerializer(typeof(Gpx), defaultNamespace);
gpx = (Gpx)serializer.Deserialize(reader);
}
}
This example accepts any namespace, but you could easily make it filter for a specific list of known namespaces.
Oddly enough you can't solve this nicely. Have a look at the deserialize section in this troubleshooting article. Especially where it states:
Only a few error conditions lead to exceptions during the
deserialization process. The most common ones are:
•The name of the
root element or its namespace did not match the expected name.
...
The workaround I use for this is to set the first namespace, try/catch the deserialize operation and if it fails because of the namespace I try it with the next one. Only if all namespace options fail do I throw the error.
From a really strict point of view you can argue that this behavior is correct since the type you deserialize to should represent a specific schema/namespace and then it doesn't make sense that it should also be able to read data from another schema/namespace. In practice this is utterly annoying though. File extenstion rarely change when versions change so the only way to tell if a .gpx file is v0 or v1 is to read the xml contents but the xmldeserializer won't unless you tell upfront which version it will be.

Categories

Resources