• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

Asp.net c# Import XML file that contains Array of strings

Hello,

I'm importing an xml file to dataset.  Then inserting the dataset to SQL.  I can do this but not all the fields come thru.  Need help in reading the array in it.  ?Any way to call the nodes generically like node0, node1..?

Here's an example of the xml datafile
<list>
      <object-array>
            <string>191</string>
            <string>0001</string>
            <int>0</int>
      </object-array>
      <object-array>
            <string>192</string>
            <string>0001</string>
            <int>0</int>
      </object-array>
      <object-array>
            <string>193</string>
            <string>0006</string>
            <int>0</int>
      </object-array>

When I create the dataset and bind it to a grid I get this:   (the int has the correct value)

object-array_Id   int
0                             0
1                             0
2                             0
3                             0
4                             0
5                             0

Here's what I'm using for code:
 
XmlDocument doc = new XmlDocument();
DataSet ds = new DataSet();
string myurl = "http://localhost/files/V_CUSTOM_EDITS.xml";        
ds.ReadXml(myurl);
GridView1.DataSource = ds;
GridView1.DataBind();

Open in new window


This has got to be a web app.  

Any ideas?
0
jshesek
Asked:
jshesek
  • 2
2 Solutions
 
Daniel Van Der WerkenIndependent ConsultantCommented:
You need to map the XML file to a C# class. You can do this by running the XSD.EXE program that should be part of the installation for Visual Studio. Simply run it against the XML file with the /class switch, and it will create a file that is the C# class.

Then, include this class in your project. Then, what you want to do is desearialize the xml file as the class.

XSD reference: https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx

How to deserialize XML to a C# class: https://msdn.microsoft.com/en-us/library/tz8csy73(v=vs.110).aspx

Once you have all that, you'll essentially have a list of object-array objects. That can be the datasource you need.
0
 
jshesekAuthor Commented:
I actually approached it a different way.
There's only 3 fields per record.  I asked the vendor to supply a new XML record with field names instead of the data types.
Here's what I ended up with for now.
Dim x as integer = 1
Dim reader As XmlTextReader = New XmlTextReader(CustomPath)
Do While reader.Read()

    If (reader.NodeType = XmlNodeType.Text) Then
	If (x = 4) Then     ' time for new row
	    x = 1
	    rowCE = dtCustom.NewRow()
	    dtCustom.Rows.Add(rowCE)
	    y1 = y1 + 1     ' Record count 87 correct
	End If

	If (x = 1) Then
	    rowCE("I_CUST") = reader.Value.ToString()
	End If

	If (x = 2) Then
	    rowCE("C_EDIT") = reader.Value.ToString()
	End If

	If (x = 3) Then
	    rowCE("N_FLDSEQ") = reader.Value.ToString()
	End If

	x = x + 1
    End If

Loop              

Open in new window


This way it just reads it in kind of generically.

Thanks Dan7el - this did give me an idea to do if the xml record was larger.
0
 
jshesekAuthor Commented:
I ended up referencing the xml column with column count.  Since the file didn't have a lot of columns this was reasonably fast to do.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now