By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Writing a non string value to an XmlAttribute

P: n/a
Hi,
I have an XSD schema where I have attributes that are declared to
contain non string values such as integers etc. Take for example, this
declaration -

<xs:attribute name="IsThisTrue" type="xs:boolean" />

Now, I want to write or modify an XML document using the DOM API (using
C#) where this attribute will be given a boolean value. Then the
resulting XML document will be validated against the schema
programatically before saving it.

So, I want to have code that does something like this -

XmlElement xnode = (XmlElement)
sanCfgXml.SelectSingleNode("//SomeNode");
XmlElement deviceNode = sanCfgXml.CreateElement("ChildNode");
XmlAttribute attr1 = sanCfgXml.CreateAttribute("IsThisTrue");

// I'd like to do this but Value takes only a string !!
attr1.Value = true;
deviceNode.Attributes.SetNamedItem(attr1);

The problem is, how do I write typed values to this attribute ? If I do
a ToString() on the boolean value, it seems to defeat the purpose of
data types in the schema.

Is the only way to achieve this by using XmlSerialization ? Or am
missing something ?

Thanks in advance.

Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<dp******@lycos.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
I have an XSD schema where I have attributes that are declared to
contain non string values such as integers etc. : : Now, I want to write or modify an XML document using the DOM API (using
C#) where this attribute will be given a boolean value. Then the
The DOM W3C Recommendation was approved before its XML Schema
Recommendation was approved; therefore it's not surprising that there's no
support for this in that API.
// I'd like to do this but Value takes only a string !!
attr1.Value = true;
attr1.Value = true.ToString( );
If I do a ToString() on the boolean value, it seems to defeat
the purpose of data types in the schema.
If I have schema type information available in my XML consuming
application that tells me a particular XmlAttribute's Value should be
a boolean, then it's straightforward for me to write,

bool myFlag = bool.Parse( xmlAttr.Value);

But let's look at this from the other perspective. Think of how code
is affected if Value were of type System.Object?

If I don't have schema type information and Value were a System.
Object property then I have to use RTTI and start guessing what
type Value is,

bool myFlag;
int myNumber;
if ( xmlAttr.Value is bool )
myFlag = (bool)( xmlAttr.Value);
else if ( xmlAttr.Value is int )
myNumber = (int)( xmlAttr.Value);
else // . . .
// then ... did I set myFlag or myNumber or ... ???

and I have to do that on every attribute. My choices are:

1. Play this inefficient guessing game.
2. Always use a schema.
3. Always tree XmlAttribute.Value as a string, its the most conservative
data representation because every primitive type can be serialized into
a string.

Forcing developers to guess what the type is and write switching logic for it
would be excessive, and most developers would quickly tire of the repetative
switches leading them to one of the other solutions.

Requiring a schema for type information means you have to have the post
schema validation Infoset (PSVI), which means you have to have a schema
and have to schema-validate. The schema validation step is an expensive
step, so unless an application seriously requires the PSVI, its the first step
to go when tuning an application about to go into production.

That leaves the string. String is the most conservative representation, meaning
every primitive type can be serialized into a string, which means I don't have
to worry what the type is in order to process the XML document. Its easy to
go to or from a string.

Since most developers would choose to treat an Object-typed XmlAttribute
Value as a String anyway, that's its data type, as specified in the W3C's
DOM Level 1 recommendation,

http://www.w3.org/TR/1998/REC-DOM-Le...ttribute-value
Is the only way to achieve this by using XmlSerialization ?
XmlSerialization parses strings into an object's properties and then converts
an object's properties to XML by converting them into strings. You're only
gaining that you don't see what's happening.

..NET 2.0 Beta 1 has many strongly-typed methods on the XmlReader and
XmlWriter classes. Most applications would probably continue to convert
strongly-typed values into strings. However, there is the potential in 2.0 to
have intrinsically binary representations of an XML Infoset that are
non-textual.

You may also want to write your applications in Comega (C - omega), an
experimental language that treats XML as a first-class language structure.
Dare Obasanjo describes its features in this article,

http://www.xml.com/pub/a/2005/01/12/comega.html

XPathDocument is also slightly more typed then the DOM API implemented
by XmlDocument, in particular, XPath 1.0 did have the notion of the primitive
boolean type.

The drawbacks are: adoption horizon for .NET 2.0, Comega is very much a
niche language and it would be awhile before it gets adopted (if at all), and
a substantial impedance matching problem between XPath 1.0 and XML
Schema Datatypes.
Or am missing something ?


Just that DOM came before schema, and making the Value strongly-typed
would cause more hardship than letting it be a string.

Despite the existence of alternatives, ToString( ) remains the best approach
available today.
Derek Harmon
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.