Steve Hageman
Steve Hageman

Reputation: 113

XML and C# Read Header Info

I have an XML file that has some header info and then long list(s) of name / value pairs. I can read the name value pairs just fine - once I know their node names.

These are like:

<Doubler_Cal>
  <Freq>300000000</Freq> 
  <Mag>14.71725</Mag> 
</Doubler_Cal>
<Doubler_Cal>
  <Freq>310000000</Freq> 
  <Mag>15.25</Mag> 
</Doubler_Cal>
<Doubler_Cal>
  <Freq>320000000</Freq> 
  <Mag>16.25</Mag> 
</Doubler_Cal>
.
.
.
<Tripler_Stuff>
  <Freq>300000000</Freq> 
  <Mag>13.8</Mag> 
</Tripler_Stuff>
<Tripler_Stuff>
  <Freq>310000000</Freq> 
  <Mag>10.15</Mag> 
</Tripler_Stuff>
.
.
.

Etc.....

And I can read them with an XML doc reader like this,

XmlNodeList nodes=doc.DocumentElement.SelectNodes("Doubler_Cal");

or

XmlNodeList nodes=doc.DocumentElement.SelectNodes("Tripler_stuff");

No problem there..... If I know the names of these nodes (i.e. "Doubler_Cal", "Tripler_Stuff"... etc.)

The problem I am having is trying to figure out how to read the header info in the XML document that I have. In the header is a list of the value pairs names that the XML document contains.

The header looks like this (See complete snippet below)...

The node names I'm trying to parse here would be the,

<xs:element name="Doubler_Cal">
<xs:element name="Tripler_Stuff">
<xs:element name="This_too_is_possible">
<xs:element name="YetAnotherName">

to yield a string array containing: "Doubler_Cal", "Tripler_Stuff", "This_too_is_possible", "YetAnotherName".

Once I know these, I can use my "one working" line of code to get all the value pairs for that particular name.

Here is a post of the more complete XML file - It does not contain all the value pairs as there would be 100's of these. But should be enough to figure out what is going on.

<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Doubler_Cal">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Freq" type="xs:double" />
                <xs:element name="Mag" type="xs:double" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="Tripler_Stuff">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Freq" type="xs:double" />
                <xs:element name="Mag" type="xs:double" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="This_too_is_possible">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Freq" type="xs:double" />
                <xs:element name="Mag" type="xs:double" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="YetAnotherName">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Freq" type="xs:double" />
                <xs:element name="Mag" type="xs:double" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Doubler_Cal>
    <Freq>300000000</Freq>
    <Mag>14.71725</Mag>
  </Doubler_Cal>
  <Doubler_Cal>
    <Freq>310000000</Freq>
    <Mag>15.25</Mag>
  </Doubler_Cal>
  <Doubler_Cal>
    <Freq>320000000</Freq>
    <Mag>16.25</Mag>
  </Doubler_Cal>
  <Tripler_Stuff>
    <Freq>300000000</Freq>
    <Mag>13.8</Mag>
  </Tripler_Stuff>
  <Tripler_Stuff>
    <Freq>310000000</Freq>
    <Mag>10.15</Mag>
  </Tripler_Stuff>
  <This_too_is_possible>
    <Freq>322000000</Freq>
    <Mag>17.25</Mag>
  </This_too_is_possible>
  <This_too_is_possible>
    <Freq>332000000</Freq>
    <Mag>18.8</Mag>
  </This_too_is_possible>
  <This_too_is_possible>
    <Freq>334000000</Freq>
    <Mag>19.15</Mag>
  </This_too_is_possible>
  <YetAnotherName>
    <Freq>322000000</Freq>
    <Mag>17.25</Mag>
  </YetAnotherName>
  <YetAnotherName>
    <Freq>332000000</Freq>
    <Mag>18.8</Mag>
  </YetAnotherName>
</NewDataSet>

Thanks for the help, I appreciate it.

Upvotes: 0

Views: 2605

Answers (1)

Mitch
Mitch

Reputation: 22281

Based off of context, it looks like you are working with an XML File saved with DataSet.WriteXml(string, XmlWriteMode.WriteSchema). You should read this file with DataSet.ReadXml. Each of the value-pairs are rows in tables.

Example:

var rhDs = new DataSet();
rhDs.ReadXml("dat.xml");

foreach (DataTable table in rhDs.Tables)
{
    Console.WriteLine("Table: {0}\r\n", table.TableName);

    foreach (DataColumn column in table.Columns)
    {
        Console.Write("{0}\t", column.ColumnName);
    }
    Console.WriteLine("\r\n-----------------------------------------");

    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            Console.Write("{0}\t", row[column]);
        }
        Console.WriteLine();
    }

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

Prints:

Table: Doubler_Cal
Freq    Mag
-----------------------------------------
300000000       14.71725
310000000       15.25
320000000       16.25


Table: Tripler_Stuff
Freq    Mag
-----------------------------------------
300000000       13.8
310000000       10.15


Table: This_too_is_possible
Freq    Mag
-----------------------------------------
322000000       17.25
332000000       18.8
334000000       19.15


Table: YetAnotherName
Freq    Mag
-----------------------------------------
322000000       17.25
332000000       18.8


Parsing Manually

If you are not working with a saved dataset, the information you present looks like an embedded XML schema, but it is not well formed. You would therefore have some problems getting it to parse by a schema parser. You will have to reference the namespace which must be present elsewhere in the file.

Prints:

Doubler_Cal
Tripler_Stuff
This_too_is_possible
YetAnotherName

You can also do this with XmlDocument via:

var xd = new XmlDocument();
xd.Load("XMLFile1.xml");

var names = new XmlNamespaceManager(xd.NameTable);
names.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
var elementNames = xd.SelectNodes("/NewDataSet/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/@name", names);

foreach (XmlAttribute name in elementNames)
{
    Console.WriteLine(name.Value);
}

Upvotes: 3

Related Questions