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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:27:18+00:00 2026-05-23T11:27:18+00:00

I am using XmlReader to read through an xml file. I do not have

  • 0

I am using XmlReader to read through an xml file. I do not have control over the xml file and sometimes the xml file skips elements as shown below.

<Part xsi:type="Part1">
  <Quantity>1</Quantity>
  <Drawing>10</Drawing>
</Part>
<Part xsi:type="Part2">
  <Quantity>1</Quantity>
</Part>

The Drawing and Weight are in the first entry (Part 1) but not in the second one (Part 2).
This xml has 1300 entries (Part’s) and not all entries (Part’s) have all elements as above.

My XmlReader code is below.

private void Select(string elem, ArrayList al)
{
 using (XmlReader reader = new XmlTextReader(xml))
 {
  while (reader.Read())
  {
    if (reader.NodeType == XmlNodeType.Element)
    {
        if (reader.Name == elem && reader.IsStartElement())
        {
            string output = reader.ReadString();

            Console.WriteLine(output);
        }}}}}

How would I handle adding an element to the Arraylist when the element is not in the entry (Part) in the xml?

Update:
I modified the code to just be an output and here is the expected output:
1
10
1
” ”

I would like the output of Part 2 to display strings with a space " " for missing elements like Drawing.

  • 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-23T11:27:18+00:00Added an answer on May 23, 2026 at 11:27 am

    I think you can try a syntax vwery close to this example:

    Reading XML through LINQ

    you can test existence of a node with something like this:

    XDocument document = XDocument.Load("file.xml");
    
    foreach (XElement item in document.Descendants("Part"))
    {
        if (item.Element("Quantity") == null)
             al.add(string.Empty);
        else
             al.add(item.Element("Quantity").value);
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using XMLReader to read a large XML file with about 1 million
I'm having some trouble using XmlReader to read an XML file. I can open
I want to read a XML file, using XMLReader but the END ELEMENT is
I have the following two errors when using XMLReader. 1) Warning: XMLReader::read() [xmlreader.read]: MyXML.xml:43102:
I am using the code below to read a ~2.5Gb Xml file as fast
I am using XmlReader in .NET to parse an XML file using a loop:
I read xml-string using XmlReader with code: XmlReader reader = XmlReader.Create(new StringReader(xmlString)); while (reader.Read())
I'm using an XmlReader.ReadInnerXML to read an XML document (as text) embedded within in
I'm trying to read an xml document but the XmlReader.ReadToNextSibling does not work as
I'm using an XmlReader to iterate through some XML. Some of the XML is

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.