?
Solved

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

Posted on 2015-02-13
3
Medium Priority
?
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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