3 Replies - 660 Views - Last Post: 15 October 2016 - 03:03 PM Rate Topic: -----

#1 cosmarchy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-December 14

Best Way to Read XML With Variable Elements

Posted 13 October 2016 - 01:56 PM

Hi,

I'm looking for advice as to the best way to get an xml file into a dataset. I am presented with a xml file from our application which I need to get in to a dataset which I already have broadly mapped out - using xmlDocument, iterate through adding each elements data in to the dataset (over generalization).

The problem is that each time the xml will have different elements; something like this:
one time it may be like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog xmlns:dt="urn:schemas-microsoft-com:datatypes">
<Rec>
<ABC1 dt:dt="string">ADB1 DATA</ABC1>
<ABC2 dt:dt="string">ABC2 DATA</ABC2>
<ABC3 dt:dt="string">ABC3 DATA</ABC3>
<ABC4 dt:dt="string">ABC4 DATA</ABC4>
<ABC5 dt:dt="string">ABC5 DATA</ABC5>
<ABC6 dt:dt="string">ABC6 DATA</ABC6>
<ABC7 dt:dt="string">ABC7 DATA</ABC7>
</Rec>
</Catalog>

and another time it could be like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog xmlns:dt="urn:schemas-microsoft-com:datatypes">
<Rec>
<ABC1s dt:dt="string">ADB1s DATA</ABC1s>
<ABC2 dt:dt="string">ABC2 DATA</ABC2>
<ABC3 dt:dt="string">ABC3 DATA</ABC3>
<ABC4 dt:dt="string">ABC4 DATA</ABC4>
<ABC5 dt:dt="string">ABC5 DATA</ABC5>
<ABC6 dt:dt="string">ABC6 DATA</ABC6>
<ABC7 dt:dt="string">ABC7 DATA</ABC7>
</Rec>
</Catalog>

or perhaps this
<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog xmlns:dt="urn:schemas-microsoft-com:datatypes">
<Rec>
<ABC1 dt:dt="string">ADB1 DATA</ABC1>
<ABC2 dt:dt="string">ABC2 DATA</ABC2>
<ABC6 dt:dt="string">ABC6 DATA</ABC6>
<ABC7 dt:dt="string">ABC7 DATA</ABC7>
</Rec>
</Catalog>


clearly I need to determine which variant it is before trying to add data as the dataset fields may not exist if it is the wrong xml so what is the best way to determine which elements are available containing data?

My initial thoughts are whether you could validate the xml against a list of schema to see whether one matched from which you would know which elements are available, but I'm not sure whether you can do this or whether it is the best thing to do.

If anyone has any ideas on the best way to achieve this?

Is This A Good Question/Topic? 0
  • +

Replies To: Best Way to Read XML With Variable Elements

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7241
  • View blog
  • Posts: 24,554
  • Joined: 05-May 12

Re: Best Way to Read XML With Variable Elements

Posted 13 October 2016 - 02:51 PM

What happens when you simply use DataSet.ReadXml()?
Was This Post Helpful? 0
  • +
  • -

#3 cosmarchy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-December 14

Re: Best Way to Read XML With Variable Elements

Posted 15 October 2016 - 01:45 PM

View PostSkydiver, on 13 October 2016 - 02:51 PM, said:

What happens when you simply use DataSet.ReadXml()?


I do get the xml imported into the dataset but I don't think I can use ReadXML() because some of the data contains comma delimited data which I need to do something with as I iterate through and I don't think there is anything built in to do this.

So for example, I could have the following XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog xmlns:dt="urn:schemas-microsoft-com:datatypes">
<Rec>
<ABC1 dt:dt="string">ADB1 DATA</ABC1>
<ABC2 dt:dt="string">ABC2 DATA</ABC2>
<ABC3 dt:dt="string">ABC3 DATA</ABC3>
<ABC4 dt:dt="string">ABC4 DATA</ABC4>
<ABC5 dt:dt="string">ABC5 DATA</ABC5>
<ABC6 dt:dt="string">ABC6 DATA</ABC6>
<ABC7 dt:dt="string">ABC7A DATA,ABC7B DATA,ABC7C DATA</ABC7>
</Rec>
</Catalog>


so I'll need to end up with the following in the datasets:
ADB1 DATA, ABC2 DATA, ABC3 DATA, ABC4 DATA, ABC5 DATA, ABC6 DATA
and another dataset (linked to the first):
ABC7A DATA
ABC7B DATA
ABC7C DATA

not sure how best to do this without manually iterating through and processing each record....
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7241
  • View blog
  • Posts: 24,554
  • Joined: 05-May 12

Re: Best Way to Read XML With Variable Elements

Posted 15 October 2016 - 03:03 PM

I think that you can designed a set of XSD's that you can validate against so that you can determine which flavor of data you are dealing with.

My instincts say that you are correct that you will still have to manually parse some of the element TEXT values given that new wrinkle that you added.

On the plus side, since you will likely have to define some regular expressions in the XSD's to indicate what are valid TEXT values, you can just recycle the same regular expressions when it is time to parse.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1