0

I'm currently trying to read an XML file of this format to transform it into a list but trying out the code in the comments gave me an error: this is how it looks like in IE. Obviously theres a close assets tag and close properties later

  <Properties>
- <Assets>
- <Asset Name="" Version="">
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" />  
  </Asset>

So I did this:

XmlReader xReader = XmlReader.Create(new StringReader(xmlDoc));

where xml doc = @"\\visreP01\REFERENCES\default.reference.versions\default.reference.versions.properties.xml"

I'm planning to test how to iterate a name a version and so on to place them in a list iteratively. but during the reading the I got the data root level is invalid line 1 position 1.

PhillipsJP
  • 47
  • 8
  • 1
    Your XML is invalid. Correct it. – Alexander Petrov Jun 06 '16 at 00:47
  • Have you tried or anything yet? Have you looked for any pre-existing answers? E.g. [How to Deserialize XML document](https://stackoverflow.com/questions/364253) or [How to deserialize xml to object](https://stackoverflow.com/questions/10518372) or [Convert XML String to Object](https://stackoverflow.com/questions/3187444) or [How does one parse XML files?](https://stackoverflow.com/questions/55828) or [How do I read and parse an XML file in C#?](https://stackoverflow.com/questions/642293) – dbc Jun 06 '16 at 03:21
  • 1
    Not on a dev pc atm but XDocument with a loop and some queries will do the trick – Jester Jun 06 '16 at 03:28

1 Answers1

0

Try this

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            XmlReader xReader = XmlReader.Create(FILENAME);

            while(!xReader.EOF)
            {
                if (xReader.Name != "Asset")
                {
                    xReader.ReadToFollowing("Asset");
                }
                if (!xReader.EOF)
                {
                    XElement assets = (XElement)XElement.ReadFrom(xReader);
                    var results = assets.Elements("TestCase").Select(x => new
                    {
                        name = (string)x.Attribute("Name"),
                        version = (string)x.Attribute("Version"),
                        subVersion = (string)x.Attribute("SubVersion")
                    }).ToList();


                 }
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I'm trying to use this solution but I get a Data at the root level is invalid. Line 1, position 1. I'm trying to target a XML file that is located somewhere on my computer. Ill edit my main post to show you tell me what's wrong – PhillipsJP Jun 06 '16 at 12:56
  • Make sure the xml file starts on doesn't have any spaces at the beginning. You can validate xml in VS doing the following in the menus : 1) Project : Add New Item : XML File. Paste xml into view. Errors will show up in Error List like any other compiler error. – jdweng Jun 06 '16 at 13:14
  • Do you have the identification line in the file : . – jdweng Jun 06 '16 at 13:16
  • yes i do actually it shows this when I open IE: THIS FIRST -> then it starts - – PhillipsJP Jun 06 '16 at 13:22
  • If you are reading from a file you only need : XmlReader xReader = XmlReader.Create(FILENAME); StringReader not required. – jdweng Jun 06 '16 at 13:25
  • thanks for the help removing the stringreader removed that error now my next question would be when I'm debugging to check if for example aName and aVersion or whichever contain the information I'm looking for it does not. the asset itself has it but not the variable. are we using the wrong property? they contain null currently – PhillipsJP Jun 06 '16 at 13:32
  • I have another question regarding this issue. How can I modify this code to capture more than one test case per asset? – PhillipsJP Jun 06 '16 at 18:00
  • http://stackoverflow.com/questions/37664143/how-do-i-modify-the-code-to-read-multiple-testcase-tags-in-an-asset-tag – PhillipsJP Jun 06 '16 at 18:12
  • Updated code to match latest xml and latest request. – jdweng Jun 06 '16 at 19:04
  • actually I'm not sure how to use this in my link above, how would I add them to my directories subdirectories – PhillipsJP Jun 06 '16 at 19:35
  • Answered in your other posting. – jdweng Jun 06 '16 at 21:23