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 81075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:24:09+00:00 2026-05-10T21:24:09+00:00

What is the best way to deal with XML documents, XSD etc in C#

  • 0

What is the best way to deal with XML documents, XSD etc in C# 2.0?

Which classes to use etc. What are the best practices of parsing and making XML documents etc.

EDIT: .Net 3.5 suggestions are also welcome.

  • 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. 2026-05-10T21:24:09+00:00Added an answer on May 10, 2026 at 9:24 pm

    The primary means of reading and writing in C# 2.0 is done through the XmlDocument class. You can load most of your settings directly into the XmlDocument through the XmlReader it accepts.

    Loading XML Directly

    XmlDocument document = new XmlDocument(); document.LoadXml('<People><Person Name='Nick' /><Person Name='Joe' /></People>'); 

    Loading XML From a File

    XmlDocument document = new XmlDocument(); document.Load(@'C:\Path\To\xmldoc.xml'); // Or using an XmlReader/XmlTextReader XmlReader reader = XmlReader.Create(@'C:\Path\To\xmldoc.xml'); document.Load(reader); 

    I find the easiest/fastest way to read an XML document is by using XPath.

    Reading an XML Document using XPath (Using XmlDocument which allows us to edit)

    XmlDocument document = new XmlDocument(); document.LoadXml('<People><Person Name='Nick' /><Person Name='Joe' /></People>');  // Select a single node XmlNode node = document.SelectSingleNode('/People/Person[@Name = 'Nick']');  // Select a list of nodes XmlNodeList nodes = document.SelectNodes('/People/Person'); 

    If you need to work with XSD documents to validate an XML document you can use this.

    Validating XML Documents against XSD Schemas

    XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidateType = ValidationType.Schema; settings.Schemas.Add('', pathToXsd); // targetNamespace, pathToXsd  XmlReader reader = XmlReader.Create(pathToXml, settings); XmlDocument document = new XmlDocument();  try {     document.Load(reader); } catch (XmlSchemaValidationException ex) { Trace.WriteLine(ex.Message); } 

    Validating XML against XSD at each Node (UPDATE 1)

    XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidateType = ValidationType.Schema; settings.Schemas.Add('', pathToXsd); // targetNamespace, pathToXsd settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);  XmlReader reader = XmlReader.Create(pathToXml, settings); while (reader.Read()) { }  private void settings_ValidationEventHandler(object sender, ValidationEventArgs args) {     // e.Message, e.Severity (warning, error), e.Error     // or you can access the reader if you have access to it     // reader.LineNumber, reader.LinePosition.. etc } 

    Writing an XML Document (manually)

    XmlWriter writer = XmlWriter.Create(pathToOutput); writer.WriteStartDocument(); writer.WriteStartElement('People');  writer.WriteStartElement('Person'); writer.WriteAttributeString('Name', 'Nick'); writer.WriteEndElement();  writer.WriteStartElement('Person'); writer.WriteStartAttribute('Name'); writer.WriteValue('Nick'); writer.WriteEndAttribute(); writer.WriteEndElement();  writer.WriteEndElement(); writer.WriteEndDocument();  writer.Flush(); 

    (UPDATE 1)

    In .NET 3.5, you use XDocument to perform similar tasks. The difference however is you have the advantage of performing Linq Queries to select the exact data you need. With the addition of object initializers you can create a query that even returns objects of your own definition right in the query itself.

        XDocument doc = XDocument.Load(pathToXml);     List<Person> people = (from xnode in doc.Element('People').Elements('Person')                        select new Person                        {                            Name = xnode.Attribute('Name').Value                        }).ToList(); 

    (UPDATE 2)

    A nice way in .NET 3.5 is to use XDocument to create XML is below. This makes the code appear in a similar pattern to the desired output.

    XDocument doc =         new XDocument(               new XDeclaration('1.0', Encoding.UTF8.HeaderName, String.Empty),               new XComment('Xml Document'),               new XElement('catalog',                     new XElement('book', new XAttribute('id', 'bk001'),                           new XElement('title', 'Book Title')                     )               )         ); 

    creates

    <!--Xml Document--> <catalog>   <book id='bk001'>     <title>Book Title</title>   </book> </catalog> 

    All else fails, you can check out this MSDN article that has many examples that I’ve discussed here and more. http://msdn.microsoft.com/en-us/library/aa468556.aspx

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

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There are several options, but it does somewhat depend on… May 11, 2026 at 11:28 am
  • added an answer Found the solution. public partial class Group { public ObjectQuery<Member>… May 11, 2026 at 11:28 am
  • added an answer I ended up using a little server-side preprocessing. This site,… May 11, 2026 at 11:28 am

Related Questions

What is the best way to deal with XML documents, XSD etc in C#
What is the best way to deal with storing and indexing URL's in SQL
What is the best way to verify/test that a text string is serialized to
What is the best way to authorize all users to one single page in
What is the best way to store international addresses in a database? Answer in
What is the best way to include an html entity in XSLT? <xsl:template match=/a/node>
What is the best way to iterate through a strongly-typed generic List in C#.NET
What is the best way to manage a list of windows (keeping them in
What is the best way to create redundant subversion repositories? I have a subversion
What is the best way to record statistics on the number of visitors visiting

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.