Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:57:14+00:00 2026-05-31T22:57:14+00:00

I’m working on some code that loads an xml file at run time. At

  • 0

I’m working on some code that loads an xml file at run time. At the minute, we’re using the XmlDocument type to read the xml file and wrapping a try-catch around SelectSingleNode statement (this is done on the off chance that a node is null, or isn’t there as we’re parsing user created xml files).

Please note: I realise that XmlDocument has been replaced by XDocument. However since we’re working with .NET version 3 (according to this MSDN document XDocument isn’t available in .NET 3), we’re having to stick with XmlDocument for now. We’re using .NET 3 for a variety of reasons (some of which are spec related).

Here’s an example of what we’re doing at the minute:

private void LoadUserXMLFile ()
{
    XmlDocument xDoc = new XmlDocument();
    XmlTextReader reader = new XmlTextReader(fileName);
    reader.Read();
    xDoc.Load(reader);

    try { firstElementString = xDoc.SelectSingleNode(<path to node>).InnderText);
    catch { <exception handling here > }
    //more SelectSingleNode statements, each wrapped inside
    //individual try-catch blocks
}

Obviously the above is an example, and I’ve simplified the catch statement.

I’ve written a schema for the user generated XML files that this app will work with, and I was wondering if I used the schema (in some way) during parsing of an XML document, would I still need to wrap each SelectSingleNode with try-catch statements?

Is it even possible to use the schema (in some way) during parsing to check the XML document has the correct format and all of the required elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:57:15+00:00Added an answer on May 31, 2026 at 10:57 pm

    Yes you need to use a validating reader

    You can use something like this

    XmlTextReader r = new XmlTextReader("C:\\Xml\\MyXmlFile.xml");
    v.ValidationType = ValidationType.Schema;
    

    Obviously your xml will refer to the schema so thats how the reference to that is resolved (in the xml itself) like so

    <Employee EmployeeId="12345566" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="Employee.xsd">
       <PersonName>Krystan</PersonName>
    </Employee>
    

    if you cannot change the xml you can use XmlReaderSettings like this

    public void SomeMethod()
    {
        XmlReaderSettings xmlsettings = new XmlReaderSettings();
        xmlsettings.Schemas.Add("http://www.company.com/blah", "blah.xsd");
        xmlsettings.ValidationType = ValidationType.Schema;
        xmlsettings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
    
        XmlReader reader= XmlReader.Create("somefile.xml", xmlsettings);
    
        while (reader.Read()) { }
    }
    
    public void ValidationHandler(object sender, ValidationEventArgs args)
        void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
    {
        if (e.Severity == XmlSeverityType.Warning)
        {
            Console.Write("WARNING: ");
            Console.WriteLine(e.Message);
        }
        else if (e.Severity == XmlSeverityType.Error)
        {
            Console.Write("ERROR: ");
            Console.WriteLine(e.Message);
        }
    }
    

    to answer your question if you have validated against a schema and are selecting nodes known to be there you can dispense with the per node exception handeling but obviously you should guard against an exception that may come about because of file load etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm parsing an XML file, the creators of it stuck in a bunch social
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.