2

I am trying to read and store data from an xml file. I have been reading about various methods to read the data such as XmlReader, XmlTextReader, LinQ, etc. My XML file is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <circuit name="local">
      <Device>device1</Device>
      <Point>point1></Point>
    </circuit>
    <circuit name ="remote">
      <Device>device2</Device>
      <Point>point2</Point>
    </circuit>
</configuration>

I am trying to extract Device and Point set so I can pass those along to be used in a database query. I used this code and the foreach loop to verify the contents, but it only gets the first set.

XDocument msrDoc = XDocument.Load("BNOC MSR.config");
var data = from item in msrDoc.Descendants("circuit")
           select new
           {
               device = item.Element("Device").Value,
               point = item.Element("Point").Value
           };
foreach (var p in data)
   Console.WriteLine(p.ToString());

I have also tried this, but my arrays were all null

 String[] deviceList = new String[1];
 String[] pointList = new String[1];
 int n = 0;

 XmlDocument msrDoc = new XmlDocument();
 msrDoc.Load("BNOC MSR.config");
 var itemNodes = msrDoc.SelectNodes("circuit");
 foreach (XmlNode node in itemNodes)
 {
     var circuit = node.SelectNodes("circuit");
     foreach (XmlNode cir in circuit)
     {
         deviceList[n] = cir.SelectSingleNode("Device").InnerText;
         pointList[n] = cir.SelectSingleNode("Point").InnerText;
     }
 }

Any help would be greatly appreciated.

alexsious
  • 23
  • 4
  • Two notes: a) Your XML is technically invalid (stray > on your first point that should be escaped as >) and b) in VS2017/.NET 4.6.2 I can't reproduce the issue - I get both elements using the first block of code you pasted. What version of .NET are you using? I see you tagged it with VS2008, which is... well, to be quite honest... old. – GalacticCowboy May 07 '18 at 20:20
  • First code snippet looked good. I pasted into VS and it seems to be working, I am getting output from both nodes. – Keith May 07 '18 at 20:23

3 Answers3

0

Are you sure you don't want to use the built-in Properties.Settings for this?

Circuit local = Properties.Settings.Default.localCircuit;
Circuit remote = Properties.Settings.Default.remoteCircuit;

https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings

Rkand
  • 471
  • 2
  • 9
  • I like this, however is there a way to iterate this as the number of circuits fluctuates with time? – alexsious May 08 '18 at 18:08
  • Yes, you have to edit the settings file though. Check out the approved answer in this comment:https://stackoverflow.com/questions/1766610/how-to-store-int-array-in-application-settings – Rkand May 10 '18 at 17:40
0

I believe there is something wrong with the way you are testing the result. The code:

void Main()
{
    var fileLocation = @"C:\BrianTemp\input.txt";
    var xml = File.ReadAllText(fileLocation);

    XDocument msrDoc = XDocument.Load(fileLocation);
    var data = from item in msrDoc.Descendants("circuit")
               select new
               {
                   device = item.Element("Device").Value,
                   point = item.Element("Point").Value
               };
    foreach (var p in data)
    {
        //It is best practice to use statement blocks {} to prevent silly errors.  
        //Sometimes you want to execute multiple statements, especially as code changes later
        Console.WriteLine($"{p}");
    }

}

Produces the expected output:

{ device = device1, point = point1 }
{ device = device2, point = point2 }

You said:

I used this code and the foreach loop to verify the contents, but it only gets the first set.

As you can see the code produces 2 results as it should.

Note: I corrected the XML file to remove the extra >

<Point>point1></Point><==

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
0

I see two problems in your code (and I only tried the second method you posted):

  1. Your string arrays are too small, change to:

    String[] deviceList = new String[1]; String[] pointList = new String[1];

  2. The line var itemNodes = msrDoc.SelectNodes("circuit"); should be

    var itemNodes = msrDoc.SelectNodes("configuration");

JayV
  • 3,238
  • 2
  • 9
  • 14