|
Now I've tried that but no difference
_____________________________
...and justice for all
APe
|
|
|
|
|
|
Hi,
c#, vs2005.
Im creating xml documents and later validating them using an xsd.
I use the following code to create and append elements then output the xml document to a file:
XmlElement element = xmlDoc.CreateElement(fieldName);
element.InnerText = fieldValue;
parentElemennt.AppendChild(element);
the document is saved to file using xmlDoc.Save(path...)
and validated later using an XmlReader with reader settings.
The problem is that sometimes the fieldValue can be a blank string - looking at the xml file the empty tags get written as:
<element>
</element>
This causes problems with validation where lets say the type is set to a maxLength of 2:
The value '
' is invalid according to its datatype. The actual length is greater than the MaxLength value.
<element></element> works fine if i change it in the xml document manually.
How can I get the xml to be written as <element></element> or <element /> without the carriage return?
Another validation error im getting is where the field type is an Integer but left blank.
The value '
' is invalid according to its datatype The string ' ' is not a valid Integer value.
With this i still get an error if I change the xml to <element></element> and I have added the nillable=true flag in the xsd.
How can I get blank numeric elements to get accepted?
Thanks in advance.
Chas
|
|
|
|
|
Check to see if there is actually anything in fieldValue (i.e. is it an empty string) before setting the innerText. Make sure the string has been trimmed before the test so that you don't get dodgy data issues creeping in.
|
|
|
|
|
<field name="textBox" y="3.175" x="3.17" w="62" h="9">
<ui>
<textBox>
<border>
<css style=""> </css>
</border>
<margin/>
</textBox>
</ui>
<font face="Arial"/>
<margin topInset="1" bottomInset="1" leftInset="1" rightInset="1"/>
<textAlign vAlign="middle"/>
<label reserve="25">
<Font fontFace="Arial" size="8pt" />
<textAlign vAlign="middle"/>
<value>
<text>textbox label</text>
</value>
</label>
</field>
So I am trying to read the values of Font (IF there is a textBox element present) and trying to read the attributes fontFace and size, here is my Xpath:
XPathNodeIterator iterator = nav.Select(@"/field");
try
{
while (iterator.MoveNext())
{
...
XPathNavigator nav2 = iterator.Current.Clone();
if (string.Compare(nav2.Name, "field") == 0)
{
XPathNodeIterator textBoxIterator = nav2.Select("child::*/child::textBox");
if (textBoxIterator.Count > 0)
{
XPathExpression exp = nav2.Compile("child::Font");
XPathNodeIterator it = nav2.Select(exp);
while (it.MoveNext())
{
XPathNavigator nav3 = it.Current.Clone();
string fontFace = nav3.GetAttribute("typeface", nav3.NamespaceURI);
string fontSize = nav3.GetAttribute("size", nav3.NamespaceURI);
}
}
}
}
catch
...
}
Am I doing it the proper way or is it too complicated and can be simplified?
modified on Tuesday, March 4, 2008 10:22 AM
|
|
|
|
|
//field/font@fontFace I believe, and you can put something in [] to specific a condition, such as where there's a sibling node called textbox.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi,
I have a problem with retrieving a wav file from the Resources folder.
The wav is file has it build action stands on Embedded Resource.
I thought to acces a png file I can use:
Bitmap bm = new Bitmap(GetType().Assembly.GetManifestResourceStream("image.png"));
So what to use to access a .wav file?
Stream wav = System.Reflection.Assembly.GetManifestResourceStream("attention.wav"); ?
Thanks
|
|
|
|
|
System.IO.Stream s = Properties.Resources.attention; // the name of the resource
Eslam Afifi
|
|
|
|
|
Stream wav = Assembly.GetExecutingAssembly().<br />
GetManifestResourceStream(<br />
MethodBase.GetCurrentMethod().<br />
DeclaringType.Namespace + ".Resources." + "attention.wav");
(MethodBase.GetCurrentMethod().DeclaringType.Namespace assuming your calling from within the same namespace)
Answered this for you in this thread[^]
Dave
modified on Wednesday, March 5, 2008 4:22 AM
|
|
|
|
|
Hello everybody
My project is a C# project on VS2005.
My application communicate with external device through Serial Port.I'm using USB to RS convertor whitch add another COM Port in my computer.When I connect with device through this COM Port(transmit and receive data) everythink is OK.When unplug the USB cable from PC and I try to transmit data the program is generate exeption.How can I detect when the COM Port is disappear?
Thanks in advance
|
|
|
|
|
Hi,
I beleive you are looking for .IsOpen
I've just tried this with a USB-Serial converter and it works. (Goes false on a disconnect).
It doesn't cause an event though.
Matthew Butler
|
|
|
|
|
Sorry but it doesn't works.
My code is
if (sp.IsOpen)
{
sp.Write(TxBuf, 0, (k + 2 + ofs));
}
else
{
FL_Connected = false;
MessageBox.Show("Message");
}
When I unplug USB cable (Converter) from my computer the COM Port is still yet open but when the program try to write data to serial port it's generate exception "Access to the Port is denied"
|
|
|
|
|
You should always put communication like this in a try/catch block so you can catch exceptions like this.
It may be worth starting a timer when you open your port, pausing it when you're about to read/write and stopping it when you close it. That way you can test the port on each Tick event so you always know when it's connected/disconnected.
You can also monitor USB devices, get notified when one is inserted/removed and check if it's the one you're intersted in so you can act accordingly. There are some articles on this site if you search.
Dave
|
|
|
|
|
thanks for an answer
I try to do this
try
{
sp.Write(TxBuf, 0, (k + 2 + ofs));
}
catch
{
FL_Connected = false;
MessageBox.Show("Message");
}
And it works.But when I close the program it generate the same exception(Access to the port is denied)
|
|
|
|
|
Are you closing the port when you're finished with it? It sounds like you're keeping it open and the program is trying to close the port when it ends - which will generate an exception if it's not available. If you need to keep the port open for the duration, you should close the port manually in the formclosing event - and again, put that inside a try/catch block.
Dave
|
|
|
|
|
Thanks DaveyM69
I try to do this
protected override void OnClosing(CancelEventArgs e)
{
try
{
sp.Close();
}
catch
{
int probe = 33;
}
}
In this case there is no exception when close the program but the program is stoped on the breakpoint in the catch.
There is something interesting.When I start the program again the all window is white for a 2 or 3 seconds and then starts normaly.
|
|
|
|
|
Hmm... Looks like it's taking a while for it to Dispose. You could try calling Dispose() or Dispose(true) and maybe even do a GC.Collect() to force the clean up.
First thing I'd do is see what exceptions if any are occurring
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.InnerException.Message);
}
Dave
|
|
|
|
|
The problem is when I unplug the USB converter from PC and COM Port is disapear any tring of using a method of instance of serial port generate exception i.e. I can't use Sp.Dispose() or SP.Close();
|
|
|
|
|
Maybe check if it's null before doing any operations?
public static bool IsPortAvailable(System.IO.Ports.SerialPort portToTest)
{
return !(null == portToTest);
}
Dave
|
|
|
|
|
Thanks DaveyM69 and sorry to loosing your time.In this moment nothing
help.Now I will relax and try again later to decide the problem
Thanks for all
|
|
|
|
|
I'm creating an excel file using c# and the ms office object library. In a few circumstances, after I've created a new worksheet tab and filled it with data, I need to go back and delete it, but unfortunately, this isn't working. My system seems to just ignore my delete command. How can I delete a worksheet I just created?
Here's some of my code. This code would delete worksheet #25:
------------------------------------------------
using MSExcel = Microsoft.Office.Interop.Excel;
private MSExcel._Application excel;
private MSExcel._Workbook workbook;
private MSExcel._Worksheet worksheet;
private MSExcel.Sheets sheet;
worksheet = (MSExcel._Worksheet)this.workbook.Worksheets[25];
((MSExcel._Worksheet)worksheet).Delete();
--------------------------------------------------
When I run this code, the program runs through each line but it simply doesn't delete that worksheet. Any idea why?
Thanks in advance.
-Goalie35
|
|
|
|
|
Try putting null to all the columns of that row. The row will be removed.
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
Hi Laddie. Thanks for the reply.
The problem is I'm not trying to remove a single row from a worksheet but rather I'm trying to remove the entire worksheet tab.
Thanks again.
-Goalie35
|
|
|
|
|
I poked around at this and almost gave up and said it was impossible.
But it looks like all you'll have to do is turn off alerts when you delete.
Your code would look something like:
excel.Application.DisplayAlerts = false;<br />
this.workbook.Worksheets[25].Delete();<br />
excel.Application.DisplayAlerts = true;
|
|
|
|
|
That was it!
Thinking about it, it seems so obvious, yet I probably never would have thought of that. Naturally, excel wants to throw up a warning before deleting the worksheet. It seems the most obvious solutions are the ones most often overlooked.
Thanks a million.
-Goalie35
|
|
|
|