Solved

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

Posted on 2015-02-13
3
183 Views
Last Modified: 2015-07-19
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
Comment
Question by:jshesek
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 40611206
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
 
LVL 1

Assisted Solution

by:jshesek
jshesek earned 0 total points
ID: 40628992
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
 
LVL 1

Author Closing Comment

by:jshesek
ID: 40888238
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question