473,385 Members | 1,531 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.

InvalidOperationException: ReadElementContentAsString method is not supported on node type None

Hi

I get the error:
System.InvalidOperationException: The ReadElementContentAsString method is
not supported on node type None. Line 267, position 12.

when running the following code:

while (reader.Read())
{
// Move to the Email element.
reader.ReadToFollowing("Email");
string email = reader.ReadElementContentAsString();

Trace.Write("email " + email);
}

Line 267, position 12. is the end of the file, i.e. after the closing root
element tag. The code works fine up until that point, it writes out the
correct values in the trace. The problem occurs with other
ReadElementContentAs... methods too.

How do I sort this out?

Thanks
Andrew
Apr 11 '07 #1
4 12095
Hi Andrew,

This error means Reader.ReadToFollowing("Email") cannot find the correct
element. Reader will stop at the end of your file. For this reason,
ReadElementContentAsString will throw System.InvalidOperationException: The
ReadElementContentAsString method is not supported on node type None.

Have you checked whether there an element named "Email" in your XML file?
Be careful of the case sensitive.

Additionally, ReadToFollowing() method will return a bool value. If this
method has located the element, it will be true, otherwise, it will be
false.
I recommend you may make some change in your code to narrow-down the issue.

while (reader.Read())
{
// Move to the Email element.
bool flag= reader.ReadToFollowing("Email");
if (flag)
{
string email = reader.ReadElementContentAsString();
.......
}
......

Hope this helps.Please let me know if you have any further issue on this.
I'm glad to assist you.
Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 12 '07 #2
Hi Wen Yuan

Thanks for your help so far. Testing that ReadToFollowing is true first
works for me. Can you help with working out the best approach and classes to
use for the following?

I want to import a user uploaded XML file into a database. The user uses a
web form to upload the file. The file should conform to a defined schema but
it may contain errors so we want to alert the user before proceeding to
insert the data into the database. I would like to do the following:

1. xml file is uploaded using the FileUpload control, e.g.

XmlReaderSettings readerSettings = new XmlReaderSettings();
//validation flags
readerSettings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings;
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas = sc;
readerSettings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
reader = XmlReader.Create(fileUpload.FileContent, readerSettings);

2. now I have an XmlReader object with a schema so I check for validation.
As the XmlReader is a forward only reader I have to store any valid values
at the same time as checking the document for validation. This seems a
little messy.
Can I use the XmlSerializer.Deserialize method and the Validator at the same
time? The xml document looks like this:

<Accounts>
<Account ID="1003" Code="code1">
<Email>1@email.com</Email>
<FirstName>f1</FirstName>
<LastName>l1</LastName>
<AccountType>3</AccountType>
<IsActive>true</IsActive>
</Account>
<Account ID="1004" Code="code2">
<Email>2@email.co.uk</Email>
<FirstName>f2</FirstName>
<LastName>l2</LastName>
<AccountType>1</AccountType>
<IsActive>true</IsActive>
</Account>
</Accounts>

I need to loop through each Account node and call a business object method
to insert into a database if the values validate with the schema. I've
looked at the documentation but I can't find a clear way to do this. Also I
don't understand how the Deserialize interprets the fact that some
information is held in attributes rather than elements. Does it get this
information from the schema?

If I can use the Deserialize method do I create a collection of custom
objects and then loop though these and call my insert method? How would I do
this?

I hope I'm being clear here. I think my requirements are quite simple but I
am unsure on which classes are appropriate and how to use them for my
application.

Thanks in advance for you help.
Andrew
Apr 12 '07 #3
"J055" <j0**@newsgroups.nospamwrote in message
news:uM****************@TK2MSFTNGP04.phx.gbl...
Hi Wen Yuan

Thanks for your help so far. Testing that ReadToFollowing is true first
works for me. Can you help with working out the best approach and classes
to use for the following?

I want to import a user uploaded XML file into a database. The user uses a
web form to upload the file. The file should conform to a defined schema
but it may contain errors so we want to alert the user before proceeding
to insert the data into the database. I would like to do the following:

1. xml file is uploaded using the FileUpload control, e.g.

XmlReaderSettings readerSettings = new XmlReaderSettings();
//validation flags
readerSettings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings;
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas = sc;
readerSettings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
reader = XmlReader.Create(fileUpload.FileContent, readerSettings);

2. now I have an XmlReader object with a schema so I check for validation.
As the XmlReader is a forward only reader I have to store any valid values
at the same time as checking the document for validation. This seems a
little messy.
Can I use the XmlSerializer.Deserialize method and the Validator at the
same time? The xml document looks like this:

<Accounts>
<Account ID="1003" Code="code1">
<Email>1@email.com</Email>
<FirstName>f1</FirstName>
<LastName>l1</LastName>
<AccountType>3</AccountType>
<IsActive>true</IsActive>
</Account>
<Account ID="1004" Code="code2">
<Email>2@email.co.uk</Email>
<FirstName>f2</FirstName>
<LastName>l2</LastName>
<AccountType>1</AccountType>
<IsActive>true</IsActive>
</Account>
</Accounts>

I need to loop through each Account node and call a business object method
to insert into a database if the values validate with the schema. I've
looked at the documentation but I can't find a clear way to do this. Also
I don't understand how the Deserialize interprets the fact that some
information is held in attributes rather than elements. Does it get this
information from the schema?
If the XML document isn't "too large", then you can load it into an
XmlDocument and then use whatever method you like to reference the data in
the XmlDocument:

XmlDocument doc = new XmlDocument();
doc.Schemas = sc;
using (XmlReader reader = XmlReader.Create(fileUpload.FileContent,
readerSettings)) // Implements IDisposable in 2.0
{
doc.Load(reader);
}

I believe that, in this case, the nodes in the XmlDocument will be decorated
with information from the schema (the Post Schema Validation Infoset or
PSVI). I haven't used this much, but have seen evidence of this in the
debugger.

You may also want to try an XmlDataDocument if a DataSet would be of use to
you.
Good luck.
--

John Saunders [MVP]
Apr 12 '07 #4
Hi Andrew,
Thanks for John's suggestion.
>If I can use the Deserialize method do I create a collection of custom
objects and then loop >though these and call my insert method? How would I
do this?

About how to use XmlSerializer mthod, you should have to create a new class
according to your XML schema. For some information is held in attributes
rather than elements, you should have to use attribute in your class.

For example:
http://www.codeproject.com/soap/GameCatalog.asp
[Using the XmlSerializer Attributes]

However, I'm afraid this is not the best approach/Class for you to achieve
what you need. There will be a lot of hard work you have to do. In general,
XmlSerializer method is used for Web service and .net Remoting.
In short, I agree John, XMLDocument is a simple way you can consider of.

XmlDocument doc = new XmlDocument();
....
doc.Load(reader);
//doc.load() method will loop through the xmlreader, if the xml file
doesn't conform to the defined schema. The ValidationEvent will fire. You
can also traverse each element/attribue by doc object.

http://www.codeproject.com/soap/myXPath.asp
[Manipulate XML data with XPath and XmlDocument (C#)]

Hope this helps.
Sincerely,
Wen Yuan
Microsoft Online Community Support

Apr 13 '07 #5

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

Similar topics

2
by: Ralf Wahner | last post by:
Dear Masters of XSLT Could I ask you for a clue on the following question? I'd like to use XSLT to transform an XML source file to LaTeX. In the following small example the <para> Element...
5
by: Daniel Crespo | last post by:
Is there a built-in method for transforming (1,None,"Hello!") to 1,None,"Hello!"? Thanks
6
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET...
4
by: ryu | last post by:
Hi all, I have a xml document where I have to replace the value of node type that is text. For example, if the value of a node whose type is 'text' is 'Toyota', I would like it to be...
3
by: _link98 | last post by:
Running DB2 ESE V8.1.8 on WinXP. This is Fixpak 8. Have a structured-type and some methods for that type. One of my methods needs to do insert / update on tables. The type specification...
6
by: Julie | last post by:
I have a situation where I have multiple objects that aren't related in any way (no base class), but all have a couple of common methods/properties. I'm looking for a clean way to call a...
0
by: Dmitry Shuklin | last post by:
I make __value type with some private fields. I want to implement System.ISerializable for custom serialization public __value struct NativeHandle : public...
2
by: ABC | last post by:
Can I create an Interface which have a method have variate type parameters? I want create an Interface named IDataEditable which have two methods: LoadRecord and SaveRecord. As the LoadRecord...
0
by: Laquesis | last post by:
What?: I'm getting this: "Method 'CreateObject' in type 'Microsoft.Practices.EnterpriseLibrary.Data.DatabaseCustomFactory' from assembly 'Microsoft.Practices.EnterpriseLibrary.Data,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.