473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

XmlXml() or XmlRaw() or XmlText(typeof(PCDATA)) or ??

This seems something that should be easy and not uncommon... but hours
of searching have yielded nothing!

I am using XmlSerializer to map some XML (not defined by me) into some
class objects (defined by me). And it is working great for most cases.
Just have one more to handle...

One of the leaf nodes of the tree I build is "ci"... typically looks
like this:

<ci>someIdentifier</ci>

To implement that, my class "ci" has the following:

[XmlText(typeof(string))]
public string TheString = null;

Works great.

However, the XML allows for some presentation nodes inside <ci>. For
example:

<ci><msub><mi>T</mi><mn>0</mn></msub></ci> ==> gives you T with
subscript 0
<ci>&deg;C</ci> ==> gives you the typical way you'd write degrees
Celsius

To me, that presentation stuff is irrelevant. Its just a fancy
identifier. But I need to capture that string between the <ci> and the
</ci>, use it like I use "someIdentifier", and be able to serialize it
back out.

Unfortunately, that does NOT currently work... the XmlSerializer turns
the first case into a bunch of unrecognized nodes which vanish and
TheString ends up empty... and the XmlSerializer throws an exception on
the second case, because the entity "deg" is not known.

What I need is the ability to say "TheString holds all the raw XML
between <ci> and </ci>". So, something like:

[XmlXml()]
public string TheString = null;

[XmlRaw()]
public string TheString = null;

or perhaps to tell XmlText that the type of this thing is PCDATA:

[XmlText(typeof(PCDATA))]
public string TheString = null;

None of those are the answer; but I'm thinking the answer might be
similarly simple.

Suggestions?

Thanks.

Feb 3 '06 #1
2 1743
Finally figured it out... "ignore the MS documentation saying 'do not
do this'" ;^)

To override serialization behavior for a class, you can implement
IXmlSerializable and change from XmlType to XmlRoot. Pretty easy!

Then, to handle the raw text, you can do WriteRaw on the way out and
ReadInnerXml on the way in.

Just thought I'd document it for the next soul doing searches like I
was.

Feb 4 '06 #2
Ooops, almost forgot...

For a better description of this and an example, go here:

http://weblogs.asp.net/cweyer/archiv...02/205798.aspx

and a thank you also to this page:

http://www.aspnetresources.com/blog/...alization.aspx

Feb 4 '06 #3

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

Similar topics

5
by: dbsmilr | last post by:
This is what I want to do: <html> <xml id="myXml"> <book title="book1" /> <book title="book2" /> </xml> <div id="out"><!-- I would output the xml formatted nicely for the user in this...
24
by: PrezWeezy | last post by:
I am attempting to create a loop to read through each XML attribute in a node and put it into a datatable. I have created my datatable with columns that are the same name as the attributes in my...
3
by: aredz | last post by:
Hi, I want to extract the data inside an xmlData to a string. Let's say I (for example) I have this XML data: <text display-inline="yes">This<b>is</b>only<ref>a</ref><i>sample</i></text> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.