473,609 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking that a string is a valid attribute name ...

Hi

I've found the rules that cover what is a valid attribute name here:

http://www.w3.org/TR/2006/REC-xml-20060816/
2.3 Common Syntactic Constructs

Is there anything already in the dotnet framework which I can use to
test whether a string can be a valid attribute name or do I have to
roll my own implementation?

Thanks for the help!

Emma

Sep 4 '06 #1
5 2123


em************* *@fastmail.fm wrote:

I've found the rules that cover what is a valid attribute name here:

http://www.w3.org/TR/2006/REC-xml-20060816/
2.3 Common Syntactic Constructs

Is there anything already in the dotnet framework which I can use to
test whether a string can be a valid attribute name or do I have to
roll my own implementation?
The name rule is the same for element and attribute names.

If you use XmlTextWriter (in .NET 1.x) or XmlWriter (in .NET 2.0) to
produce your XML then you have methods that ensure that a valid name is
used and otherwise throw an exception, see the WriteName method
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlTextWriterCla ssWriteNameTopi c.asp>

And with .NET 2.0 if you use any of the methods to for instance write
attributes e.g. WriteAttributeS tring then name arguments passed in are
checked too.

So producing XML with correct names should not be a problem. And parsing
XML should report any name violations any way.

Does that help? Or where/when exactly do you need to check a name?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 4 '06 #2
Martin Honnen wrote:
em************* *@fastmail.fm wrote:

I've found the rules that cover what is a valid attribute name here:

http://www.w3.org/TR/2006/REC-xml-20060816/
2.3 Common Syntactic Constructs

Is there anything already in the dotnet framework which I can use to
test whether a string can be a valid attribute name or do I have to
roll my own implementation?

The name rule is the same for element and attribute names.

If you use XmlTextWriter (in .NET 1.x) or XmlWriter (in .NET 2.0) to
produce your XML then you have methods that ensure that a valid name is
used and otherwise throw an exception, see the WriteName method
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlTextWriterCla ssWriteNameTopi c.asp>

And with .NET 2.0 if you use any of the methods to for instance write
attributes e.g. WriteAttributeS tring then name arguments passed in are
checked too.
I'm using XmlWriter and WriteAttributeS tring which flagged up the
problem!

It was letting through "text!" as an attribute name and only once the
resulting XMLDocument was loaded via the written-to Stream and then
validated against my schema did the error occur.
So producing XML with correct names should not be a problem. And parsing
XML should report any name violations any way.

Does that help? Or where/when exactly do you need to check a name?
We are allowing users to define their own attributename value pairs
ultimately to be put into an XML document as attributes i.e.
attributename = "value". I want to be able to validate the string they
enter as the attributename once they enter it and if there's any of the
disallowed characters just replace them with _.

See what I mean? Thanks for responding so quickly - any ideas? I was
thinking I'd have to bundle the whole definition of what was valid into
a regular expression or something.

Emma

Sep 4 '06 #3
em************* *@fastmail.fm wrote:
Martin Honnen wrote:
em************* *@fastmail.fm wrote:

I've found the rules that cover what is a valid attribute name here:
>
http://www.w3.org/TR/2006/REC-xml-20060816/
2.3 Common Syntactic Constructs
>
Is there anything already in the dotnet framework which I can use to
test whether a string can be a valid attribute name or do I have to
roll my own implementation?
The name rule is the same for element and attribute names.

If you use XmlTextWriter (in .NET 1.x) or XmlWriter (in .NET 2.0) to
produce your XML then you have methods that ensure that a valid name is
used and otherwise throw an exception, see the WriteName method
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlTextWriterCla ssWriteNameTopi c.asp>

And with .NET 2.0 if you use any of the methods to for instance write
attributes e.g. WriteAttributeS tring then name arguments passed in are
checked too.

I'm using XmlWriter and WriteAttributeS tring which flagged up the
problem!

It was letting through "text!" as an attribute name and only once the
resulting XMLDocument was loaded via the written-to Stream and then
validated against my schema did the error occur.
This is wrong - I've just checked. It does catch the !.

So the main problem is that I need to find some way of validating
user-inputted attribute name's and replace any illegal characters with
underscore.

Any ideas for something simple otherwise I'm going to have to implement
a regular expression according to the w3.org rules - and that's going
to take me a few days to understand!

:-(

Emma

Sep 4 '06 #4


em************* *@fastmail.fm wrote:

I'm using XmlWriter and WriteAttributeS tring which flagged up the
problem!

It was letting through "text!" as an attribute name and only once the
resulting XMLDocument was loaded via the written-to Stream and then
validated against my schema did the error occur.
Is that .NET 2.0? It does report a "System.Argumen tException: Invalid
name character" for me.
Doesn't seem to do that with .NET 1.x.

>>So producing XML with correct names should not be a problem. And parsing
XML should report any name violations any way.

Does that help? Or where/when exactly do you need to check a name?


We are allowing users to define their own attributename value pairs
ultimately to be put into an XML document as attributes i.e.
attributename = "value". I want to be able to validate the string they
enter as the attributename once they enter it
That could be one in .NET 1.x with WriteName e.g. alike

static public bool IsXmlName (string nameToCheck) {
XmlTextWriter xmlWriter = new XmlTextWriter(n ew StringWriter()) ;
bool isXmlName = true;
try {
xmlWriter.Write Name(nameToChec k);
}
catch (ArgumentExcept ion e) {
isXmlName = false;
}
finally {
xmlWriter.Close ();
}
return isXmlName;
}
and if there's any of the
disallowed characters just replace them with _.

See what I mean? Thanks for responding so quickly - any ideas? I was
thinking I'd have to bundle the whole definition of what was valid into
a regular expression or something.
I don't think there is any API in .NET 1.x or .NET 2.0 that flags the
individual disallowed characters in an XML name and then offers to
replace them with some allowed character. In that case you will need to
write that character replacement routine yourself.

Regular expressions in the .NET framework support Unicode character
groups/blocks with e.g. \p{name} so that might help to write the regular
expression.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 4 '06 #5
Martin Honnen wrote:
em************* *@fastmail.fm wrote:

I'm using XmlWriter and WriteAttributeS tring which flagged up the
problem!

It was letting through "text!" as an attribute name and only once the
resulting XMLDocument was loaded via the written-to Stream and then
validated against my schema did the error occur.

Is that .NET 2.0? It does report a "System.Argumen tException: Invalid
name character" for me.
Doesn't seem to do that with .NET 1.x.

>So producing XML with correct names should not be a problem. And parsing
XML should report any name violations any way.

Does that help? Or where/when exactly do you need to check a name?

We are allowing users to define their own attributename value pairs
ultimately to be put into an XML document as attributes i.e.
attributename = "value". I want to be able to validate the string they
enter as the attributename once they enter it

That could be one in .NET 1.x with WriteName e.g. alike

static public bool IsXmlName (string nameToCheck) {
XmlTextWriter xmlWriter = new XmlTextWriter(n ew StringWriter()) ;
bool isXmlName = true;
try {
xmlWriter.Write Name(nameToChec k);
}
catch (ArgumentExcept ion e) {
isXmlName = false;
}
finally {
xmlWriter.Close ();
}
return isXmlName;
}
and if there's any of the
disallowed characters just replace them with _.

See what I mean? Thanks for responding so quickly - any ideas? I was
thinking I'd have to bundle the whole definition of what was valid into
a regular expression or something.

I don't think there is any API in .NET 1.x or .NET 2.0 that flags the
individual disallowed characters in an XML name and then offers to
replace them with some allowed character. In that case you will need to
write that character replacement routine yourself.

Regular expressions in the .NET framework support Unicode character
groups/blocks with e.g. \p{name} so that might help to write the regular
expression.
Hi

Found XmlConvert.Veri fyName and XmlConvert.Enco deName!

EncodeName just replaces anything invalid with _xNNNN_ where NNNN is
its unicode value. I can then use a simple regular expression and
Regex.Replace to replace that encoding with the underscore!

Done it - well at least enough so I can cope with anything the user
puts in without having to inform them, and with the _ rather than the
_xNNNN_ the XML doesn't look too bad if a human has a peek.

Emma

Sep 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1883
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed test( "ff*2/dd.r..ss r") #additional ..ss -invalid variable. test( "ff*$24..55/ddr") #double .. and $ -invalid number test( "ff*2/dd.r.ss r") #variable with double . -invalid variable
5
2990
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python - A Proposal As we all know, one of the best things about python and other scripting languages is dynamic typing (yes I know it has advantages and disadvantages but I will not discuss them now). Dynamic typing allows us to change types of...
67
4237
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
2
2032
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data" onSubmit="return(validateData(this))"> <input type="file" name="txtFileToUpload"> <input type="submit" name="btnAdd" value="Add" class="form_button"> </form> function checkFile(frm)
2
1554
by: Anthony LaMark | last post by:
Hi All, I am generating a (X)HTML page in an IE webbrowser control (housed by a .Net WinForm application) from a XML file using XSLT (using Msxml2.DOMDocument.4.0). When the user clicks a button, control is returned back to the WinForm application where I use mshtml to get a portion of the web page (a table to be exact). Within the table cells, there are checkboxes and textboxes that are being defined as INPUT elements (so the user...
18
7321
by: William | last post by:
I have the following javascript function that updates a scroll_list and sends the updated entry (with its index) to a server script ( i.e. http://mkmxg00/cgi/confirmUpload.pl ) for further processing: function saveText( scroll_list, t_area, listToBeUpdated ) { scroll_list.options.text = t_area.text; scroll_list.options.value= t_area.value; var req; var url = "http://mkmxg00/cgi/confirmUpload.pl";
10
16400
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not recognized as a valid Boolean. Public Sub UpdateCustomer_DashboardGraphs(ByVal sender As Object, ByVal e As System.EventArgs)
9
2962
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r"); if(!$fp) {
0
8139
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8232
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7024
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6064
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4032
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.