Click here to Skip to main content
15,896,557 members
Home / Discussions / C#
   

C#

 
GeneralRe: Check which line was clicked? Pin
Anthony Mushrow4-Nov-07 3:04
professionalAnthony Mushrow4-Nov-07 3:04 
GeneralRe: Check which line was clicked? [modified] Pin
Kristian Sixhøj4-Nov-07 3:07
Kristian Sixhøj4-Nov-07 3:07 
GeneralRe: Check which line was clicked? Pin
Gadjuka4-Nov-07 4:30
Gadjuka4-Nov-07 4:30 
GeneralRe: Check which line was clicked? Pin
Kristian Sixhøj4-Nov-07 4:44
Kristian Sixhøj4-Nov-07 4:44 
GeneralRe: Check which line was clicked? Pin
Daniel Grunwald4-Nov-07 5:03
Daniel Grunwald4-Nov-07 5:03 
AnswerRe: Check which line was clicked? Pin
Tamimi - Code4-Nov-07 1:49
Tamimi - Code4-Nov-07 1:49 
GeneralRe: Check which line was clicked? Pin
Gadjuka4-Nov-07 4:23
Gadjuka4-Nov-07 4:23 
QuestionElement is an invalid XMLNodeType Pin
jon-804-Nov-07 0:39
professionaljon-804-Nov-07 0:39 
The function below should read an xml file and parse the elements and content, and I'm updating it to read the element content and the attribute values. So far the problem is with reading element content.

When the code uses method System.Xml.XmlReader.ReadElementContentAsString(), the code raises an exception (see Error).

However when the code using the above method is commented out the code is reading xml attributes.

On another note, any guidelines (for beginners) for validating XML and creating an XSD schema for validating it? My understanding is that this is the way suggested by w3c.

Less specifically, there are other ways of validating XML, including XmlSchemaValidator class, which would be most recommended?

.NET Framework 2.0


Error
------
System.Xml.XmlException: 'Element' is an invalid XmlNodeType. //INVALID?!
at System.Xml.XmlReader.FinishReadElementContentAsXxx()
at System.Xml.XmlReader.ReadElementContentAsString()
at TestConsole1.Program.readXMLFile(String _fileName) in C:\Documents and Set
tings\user\My Documents\Visual Studio 2005\Projects\TestConsole1\TestConsole1\Pr
ogram.cs:line 79

WhiteSpace:
Element:
Text:

Element:
Text:

Element:
Text:

WhiteSpace:
System.Xml.XmlException: 'Element' is an invalid XmlNodeType.
at System.Xml.XmlReader.FinishReadElementContentAsXxx()
at System.Xml.XmlReader.ReadElementContentAsString()
at TestConsole1.Program.readXMLFile(String _fileName) in C:\Documents and Set
tings\user\My Documents\Visual Studio 2005\Projects\TestConsole1\TestConsole1\Pr
ogram.cs:line 79

WhiteSpace:
WhiteSpace:
Element:
Text:

WhiteSpace:
WhiteSpace:
Press any key to continue . . .

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TestConsole1
{
    class Program
    {
        static void Main(string[] args)
        {
            readXMLFile("sample.xml");  
        }

        static private void readXMLFile(string _fileName)
        {

            /* xml counters - not required */
            int ws = 0;
            int pi = 0;
            int dc = 0;
            int cc = 0;
            int ac = 0;
            int et = 0;
            int el = 0;
            int xd = 0;
            int tx = 0;

            // Read a document

            XmlTextReader textReader = new XmlTextReader(_fileName);

            // Read until end of file

            while (textReader.Read())
            {

                XmlNodeType nType = textReader.NodeType;

                // If node type us a declaration
                
                if (nType == XmlNodeType.XmlDeclaration)
                {

                    Console.WriteLine("Declaration:" + textReader.Name.ToString());

                    xd = xd + 1;

                }

                // if node type is a comment

                if (nType == XmlNodeType.Comment)
                {

                    Console.WriteLine("Comment:" + textReader.Name.ToString());

                    cc = cc + 1;

                }

                // if node type us an attribute

                if (nType == XmlNodeType.Attribute)
                {

                    Console.WriteLine("Attribute:" + textReader.Name.ToString());

                    ac = ac + 1;

                }

                // if node type is an element

                if (nType == XmlNodeType.Element)
                {
                    try
                    {
                        //string ec = textReader.ReadElementContentAsString();  /*COMMENTED OUT*/

                        Console.WriteLine("Element:" + textReader.Name.ToString());
                       //Console.WriteLine("Text:" + ec);  /*COMMENTED OUT*/

                        el = el + 1;
                    }
                    catch (XmlException ex)
                    {
                        Console.WriteLine(ex.GetBaseException());
                        Console.WriteLine(ex.InnerException);
                    }


                }


                // if node type is an entity\

                if (nType == XmlNodeType.Entity)
                {

                    Console.WriteLine("Entity:" + textReader.Name.ToString());

                    et = et + 1;

                }

                // if node type is a Process Instruction

                if (nType == XmlNodeType.Entity)
                {

                    //Console.WriteLine("Entity:" + textReader.Name.ToString());

                    pi = pi + 1;

                }

                // if node type a document

                if (nType == XmlNodeType.DocumentType)
                {

                    Console.WriteLine("Document:" + textReader.Name.ToString());

                    dc = dc + 1;

                }

                // if node type is white space

                if (nType == XmlNodeType.Whitespace)
                {

                    Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());

                    ws = ws + 1;

                }

                if (nType == XmlNodeType.Text)
                {

                    Console.WriteLine("Text:" + textReader.Name.ToString());
                    tx = tx + 1;
                }

            }
        }
    }
}


Jon

AnswerRe: Element is an invalid XMLNodeType Pin
pmarfleet4-Nov-07 1:49
pmarfleet4-Nov-07 1:49 
QuestionHow to Catch the 2 or 3 first char in the begining of the string Pin
Assaf823-Nov-07 23:58
Assaf823-Nov-07 23:58 
AnswerRe: How to Catch the 2 or 3 first char in the begining of the string Pin
rao raja4-Nov-07 0:08
rao raja4-Nov-07 0:08 
AnswerRe: How to Catch the 2 or 3 first char in the begining of the string Pin
Guffa4-Nov-07 0:33
Guffa4-Nov-07 0:33 
GeneralRe: How to Catch the 2 or 3 first char in the begining of the string Pin
Assaf824-Nov-07 0:54
Assaf824-Nov-07 0:54 
Questionhow to use rss feeds in desktop applications in c# Pin
rao raja3-Nov-07 23:49
rao raja3-Nov-07 23:49 
AnswerRe: how to use rss feeds in desktop applications in c# Pin
Kristian Sixhøj4-Nov-07 1:22
Kristian Sixhøj4-Nov-07 1:22 
Questioncreating 2-dim array from 1 -dim array Pin
G.K.M.3-Nov-07 22:47
G.K.M.3-Nov-07 22:47 
AnswerRe: creating 2-dim array from 1 -dim array Pin
mav.northwind3-Nov-07 22:52
mav.northwind3-Nov-07 22:52 
GeneralRe: creating 2-dim array from 1 -dim array Pin
Paul Conrad4-Nov-07 4:47
professionalPaul Conrad4-Nov-07 4:47 
AnswerRe: creating 2-dim array from 1 -dim array Pin
Paul Conrad4-Nov-07 4:47
professionalPaul Conrad4-Nov-07 4:47 
GeneralRe: creating 2-dim array from 1 -dim array Pin
G.K.M.4-Nov-07 14:10
G.K.M.4-Nov-07 14:10 
QuestionUser control Pin
M. J. Jaya Chitra3-Nov-07 22:26
M. J. Jaya Chitra3-Nov-07 22:26 
AnswerRe: User control Pin
mav.northwind3-Nov-07 23:02
mav.northwind3-Nov-07 23:02 
Questionhow can i make a web application??? Pin
phizza3-Nov-07 21:41
phizza3-Nov-07 21:41 
AnswerRe: how can i make a web application??? Pin
Christian Graus3-Nov-07 21:45
protectorChristian Graus3-Nov-07 21:45 
AnswerRe: how can i make a web application??? Pin
Paul Conrad4-Nov-07 4:49
professionalPaul Conrad4-Nov-07 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.