Solved

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

Posted on 2015-02-13
3
172 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 19

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSLT Help 12 21
Long URL in SMS 6 24
Achieve json result 2 35
Insert Powershell variable into XML 4 15
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now