-1

I cant get any display with the code below. I am simply trying to read the data from the xml file, and display it with the application. Batch Compiler will be my root node from my understanding. Eventually I will be putting the data in a csv file, but I am taking this slow to be sure and understand all the parts. Any advice would be great.

XML

 <?xml version="1.0" encoding="utf-8" ?>
<BatchCompiler>
 <batch>
  <batchid>955698</batchid>
   <transactions>
     <transaction>
       <image>..\images\955698_1_.tif</image>
        <items>
         <item>
          <values>
           <value>
            <name>Bill Base Number</name>
            <data>0002025330</data>
          </value>-<value>
            <name>acct_num2</name>
            <data/>
          </value>-<value>
            <name>Tax Year</name>
            <data>2015</data>
          </value>
        </values>
        </item>
      </items>
      </transaction>
   </transactions>
  </batch>
</BatchCompiler>

CODE:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.IO.Compression;
using System.Windows;
using System.Windows.Forms;


namespace GetZip
{
class program
{
    static void Main(string[] args)
    {
XElement xml = XElement.Load("c:\\example\\extract\\index.xml");



        var items = el.Elements("BatchCompiler").Elements("BatchCompiler").DescendantNodes();

        items = from item in el.Elements("values").Elements("values").Descendants()
                where item.Attribute("name").Value == "name"
                select item.FirstNode;

        foreach (XNode node in items)
        {
            Console.WriteLine(node.ToString());

        }
    }
}

}

  • no i recently asked about access. now that i am past that, I am having a problem reading the data ? @SimonPrice – drummerORcoder Aug 28 '15 at 21:00
  • el.Elements("BatchCompiler").Elements("BatchCompiler").DescendantNodes(); looks suspicious. – James R. Aug 28 '15 at 21:00
  • im just running up your code, and it the XML is malformed... which is why your having trouble reading the data – Simon Price Aug 28 '15 at 21:09
  • I have changed your XML to make it valid, this will now read, but your the rest of your code needs work too – Simon Price Aug 28 '15 at 21:14
  • why did you change your XML code back to the invalid markup? – Simon Price Aug 28 '15 at 21:51
  • @SimonPrice I didnt intentionally.. didnt even notice here. – drummerORcoder Aug 28 '15 at 22:02
  • With regard to this question: What happens when you step through the code in the debugger? Does your loop that contains the `WriteLine` ever execute? If not, you got nothing back in `items`, which means your selection expression is wrong. This is basic debugging technique (stepping through the code to see what happens), and once you've done so you'll have a **specific** question to ask. – Ken White Aug 28 '15 at 23:52
  • @KenWhite my code gets edited when i post bad code, and it was edited to show the poster how the XML should have been formetted. – Simon Price Aug 29 '15 at 14:17
  • @Simon: My mistake. I misread what you wrote. You meant you changed the markup for *formatting* the XML for display here; I understood it to mean you edited the XML content instead. My apologies - fixing formatting of XML or code is of course appropriate. – Ken White Aug 29 '15 at 14:31

3 Answers3

0

There's a few problems here.

  1. Your XML is invalid. You're probably getting exceptions if you're actually loading the file into memory (which, based on your previous question, you think you are). You have several unclosed tags: <batch>, <values>, <items>, and <item> are all missing closing tags in your sample document.
  2. You might run into problems having those stray text nodes scattered throughout your document (the + and - portions in your sample document).
  3. If you're trying to load an XElement from an XmlReader, don't close the XmlReader until you're done with the XElement. Better yet, wrap the XmlReader in a using block and put your usage of the XElement in that using block.
  4. You don't really even need to use an XmlReader here, just use the XElement.Load(<filepath>) method. For example, XElement el = XElement.Load("C:\\example\\extract\\index.xml");. Of course, all of this presupposes you actually get a valid XML document...
  5. Your .Elements() calls look wrong, but it's impossible to fix them in the document's current state because the document can't be parsed as is.

In the future, please try to post the specific error you're getting and the steps you've tried to resolve it so far - for example, you should be getting an exception here like

The 'values' start tag on line 10 position 6 does not match the end tag of 'transaction'. Line 20, position 6.

Look at fixing that - which really means getting a valid XML document first - and then if you're still having trouble you'll be in a better place to post a question.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
0

As Dan said your xml is invalid it should be:

<BatchCompiler>
  <batch>
   <batchid>955698</batchid>
   <transactions>
    <transaction>
     <image>..\images\955698_1_.tif</image>
     <items>
      <item>
        <values>
          <value>
           <name>Bill Base Number</name>
           <data>0002025330</data>     
          </value>
          <value>
           <name>acct_num2</name>
           <data/>
          </value>
          <value>
           <name>Tax Year</name>
           <data>2015</data>
          </value>
        </values>
      </item>
     </items>
    </transaction>
   </transactions>
  </batch>
</BatchCompiler>

And this code will list the name element values:

static void Main(string[] args)
{
    XElement xml = XElement.Load(@"c:\Temp\index.xml");

    var items = xml.Descendants("name");

    foreach (var element in items)
    {
        Console.WriteLine(element.Value);
    }

    Console.WriteLine("Finished");
    Console.ReadLine();
}
Paul Matovich
  • 1,496
  • 15
  • 20
  • Thanks.. This is working for me. My xml file is fine, I just tried to copy a portion of it while viewing the XML in the browser. – drummerORcoder Aug 28 '15 at 21:43
  • I have added the code below, attempting to get the other dat, but it seems like this is the only data getting written. I also tried to add and "&" to the for each clause but I got an error. var data = xml.Descendants("data"); foreach (var element in data) { Console.WriteLine(element.Value); } Console.WriteLine("Finished Data") Can I get all data with this way? – drummerORcoder Aug 28 '15 at 21:58
  • Well if you want all the data you might be wanting to deserialize the xml into an object graph but you could just use the XElement as you data source because you can load it from the drive as you see above, read from it, write to it, add elements, remove elements etc. then just write it back to the drive with .Save when you are done. You could get the three "value" elements and step through those and keep them as a pair of "name" and "data" – Paul Matovich Aug 28 '15 at 23:16
0

Answer above/below worked to get name values

    static void Main(string[] args)
    {

            XElement xml = XElement.Load("c:\\example\\extract\\index.xml");


           var items = xml.Descendants("name");


        foreach (var element in items) 
         {
            Console.WriteLine(element.Value);

         }

            Console.WriteLine("Finished");
            Console.ReadLine();
   }