473,385 Members | 1,944 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,385 software developers and data experts.

How to Parse Mixed Content

I've spent a while researching this and my analysis to date indicates it
can't easily be done in dotnet.

what I want to do is to take xml like this
<stuff>
<node id="1"/>Now<node id="2"/<node id="7"/>is<node id="14"/<node
id="15"/>the<node id="19"/<node id="20"/>winter<node id="21"/>
</stuff>

and extract a list with node identifiers and text (which can be whitespace
as in this example)

This seems impossible with the XMLSerializer (which is a shame as this is
embedded in a somewhat complex xml file!).

It *may* be possible with XMLReader, but I'm not too clear on how.

Any help would be much appreciated!

Iain
Apr 3 '07 #1
4 11830
Iain wrote:
what I want to do is to take xml like this
<stuff>
<node id="1"/>Now<node id="2"/<node id="7"/>is<node id="14"/<node
id="15"/>the<node id="19"/<node id="20"/>winter<node id="21"/>
</stuff>

and extract a list with node identifiers and text (which can be whitespace
as in this example)
Here is a .NET 2.0 XmlReader example:

using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element &&
xmlReader.Name == "stuff") {
while (xmlReader.Read() && xmlReader.NodeType !=
XmlNodeType.EndElement && xmlReader.Name != "stuff") {
switch (xmlReader.NodeType) {
case XmlNodeType.Element:
Console.WriteLine("Found element {0} with id: {1}.",
xmlReader.Name, xmlReader.GetAttribute("id"));
break;
case XmlNodeType.Text:
Console.WriteLine("Found text node with contents
\"{0}\"", xmlReader.Value);
break;
case XmlNodeType.Whitespace:
Console.WriteLine("Found white space \"{0}\"",
xmlReader.Value);
break;
}
}
}
}
}

Output for that sample is

Found white space "
"
Found element node with id: 1.
Found text node with contents "Now"
Found element node with id: 2.
Found white space " "
Found element node with id: 7.
Found text node with contents "is"
Found element node with id: 14.
Found white space " "
Found element node with id: 15.
Found text node with contents "the"
Found element node with id: 19.
Found white space " "
Found element node with id: 20.
Found text node with contents "winter"
Found element node with id: 21.
Found white space "
"

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 3 '07 #2
On Tue, 03 Apr 2007 16:44:51 +0200, Martin Honnen wrote:
>
Here is a .NET 2.0 XmlReader example:
Wow.

Thanks!

Do you happen to know if I can zip this into an xml tree otherwise
deserialized by XmlSerializer?

Iain
Apr 3 '07 #3
Iain wrote:
Do you happen to know if I can zip this into an xml tree otherwise
deserialized by XmlSerializer?
I am not sure what you want to achieve but I don't think there is an
easy way, you would need to implement a custom XmlReader.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 4 '07 #4
On Wed, 04 Apr 2007 13:42:17 +0200, Martin Honnen wrote:
Iain wrote:
>Do you happen to know if I can zip this into an xml tree otherwise
deserialized by XmlSerializer?

I am not sure what you want to achieve but I don't think there is an
easy way, you would need to implement a custom XmlReader.
Thanks Martin.

What I wanted to do was to use XmlSerializer to serialise and deserialise
everything except the mixed content section. Which I would do with
XmlReader having overridden the XmlSerializer for that particular class in
some way. There appeared no obvious way of doing this so I used brute
force!

I've ended up coding the whole structure up (the sample plus a half dozen
other entities) with XmlReader which is a pain. If I could have done it
with XmlSerializer it would have taken 10 mins not 2 hours.

Now I'm struggling to cope with whitespace.

Nonetheless it's all working (more or less).

Thanks again...

Iain
Apr 4 '07 #5

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

Similar topics

0
by: Swaroop Kumar | last post by:
Hi: I'm trying to write a schema that contains information as described below: 1. The first element is a mandatory fixed string. 2. The second element is a mixed content element that can...
0
by: Imiro | last post by:
Hi, i'm trying to express mixed content like this with a schema: <root>string<tag1/></root> or <root>string</root> but not <root></root> I guess it would be like this as dtd declaration...
0
by: Fender Mussel | last post by:
Hi all, Is it possible to check the content of a mixed type? I would like to define an enumeration for it. The following example works syntactically, but XMLSpy does no checks on the content at...
15
by: Bill Cohagan | last post by:
I'm trying to generate class definitions from an XSD that contains something like: <xs:complexType name="foo" mixed="true"> <xs:choice minOccurs = "0" maxOccurs="unbounded"> <xs:element name =...
1
by: Anas M. Nebuchadnezzar XXXVII | last post by:
I have an Xml file that I am trying to transform using an Xsl stylesheet. My data is in the form of: <root> ... <aaa> <bbb>Mixed content type, with <def link="words">words</def> defined in...
0
by: Werner Maier | last post by:
Hi please can anybody help me to read a WEB side with Content-Type: multipart/x-mixed-replace ("Server Push") . I am a absolutly newbie with c# .net. i have problems to read the boundarys to...
0
by: Garrek | last post by:
I have an existing ASP.Net application that must be modified to support mixed content: Latin-based languages (i.e. English) intermixed with Arabic. Our code and database assumes everything is...
2
by: phaeton123 | last post by:
I was trying to use Xquery to try to do pattern matching over mixed structured and unstructured content. For example consider the following xml fragment: ..... <article id="777">...
1
by: andrew_nuss | last post by:
Hi, Lets say I have a MIXED tag in my XML DTD with content that is going to be rendered as HMTL, as well as a <boldand <italicstag. What about whitespace? Specifically, does the whitespace in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.