Solved

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

Posted on 2015-02-13
3
185 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

734 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