XML C# Question...

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
Turbanator
Posts: 883
Joined: Wed Jun 08, 1983 7:00 am

XML C# Question...

Post by Turbanator »

Wondering if anyone can help me on this one.

I have an XML file in the following format:

Code: Select all

<product>
	<tier1>
		<id>1</id>
	</tier1>
	<tier2>
		<relation>2</relation>
	<tier2>
</product>
<product>
	<tier1>
		<id>2</id>
	</tier1>
</product>
This is being pulled into a dataset using Dataset.ReadXml. The resulting XML has 3 tables, product (empty rows), tier1 (just ids), tier2 (just relations).

To read out all the id's i can simply reference to them using:
myDS.Tables["tier1"].Rows[0].ToString();

To get all the relations, similarly I can use myDS.Tables["tier2"].Rows[0].ToString();

How do I get 2 figures out of this, the relation and it's corresponding id, so the output for the above example would be:
ID: 1 Relation: 2
ID: 2 Relation: Null

?
Doombrain
Posts: 23227
Joined: Sat Aug 12, 2000 7:00 am

Re: XML C# Question...

Post by Doombrain »

i suggest you find the answer.
Users browsing this forum: No registered users and 0 guests
sirstrongbad
Posts: 264
Joined: Tue Sep 12, 2000 7:00 am

Re: XML C# Question...

Post by sirstrongbad »

Turbanator wrote:Wondering if anyone can help me on this one.

I have an XML file in the following format:

Code: Select all

<product>
	<tier1>
		<id>1</id>
	</tier1>
	<tier2>
		<relation>2</relation>
	<tier2>
</product>
<product>
	<tier1>
		<id>2</id>
	</tier1>
</product>
This is being pulled into a dataset using Dataset.ReadXml. The resulting XML has 3 tables, product (empty rows), tier1 (just ids), tier2 (just relations).

To read out all the id's i can simply reference to them using:
myDS.Tables["tier1"].Rows[0].ToString();

To get all the relations, similarly I can use myDS.Tables["tier2"].Rows[0].ToString();

How do I get 2 figures out of this, the relation and it's corresponding id, so the output for the above example would be:
ID: 1 Relation: 2
ID: 2 Relation: Null

?


Not sure I'm understanding what your XML is trying to do, but I would go in this direction... (not accurate by any means, but might give you an idea)

Code: Select all

Dim i as Int32
Dim j as Int32
Dim _innerRow as DataRow
dim _id as String
dim _relation as String

For i = 0 to myDs.Rows.Count - 1
    _innerRow = myDs.Rows(i)
    for j = 0 to _innerRow.Rows.Count - 1
        _id = _innerRow.Rows(j)(0).ToString
        _relation = iif(_innerRow.Rows(j)(1).ToString = String.Empty, DbNull.Value, _innerRow.Rows(j)(1).ToString))
    next
Next
User avatar
MKJ
Posts: 32582
Joined: Fri Nov 24, 2000 8:00 am

Re: XML C# Question...

Post by MKJ »

i suggest you post this in T&T. cunt
inolen
Posts: 705
Joined: Thu Dec 16, 1999 8:00 am

Re: XML C# Question...

Post by inolen »

What's the purpose of the tier1/tier2 tags?

Edit: Feel free to IM me, I'm not doing much atm :)
Turbanator
Posts: 883
Joined: Wed Jun 08, 1983 7:00 am

Re: XML C# Question...

Post by Turbanator »

this is a data file that holds information i need to visualize, so i'm reverse engineering it. I've got it back to an XML form, and now I need to load data off the XML into a new dataset so I can output a "clean xml" to work with for visualization.

Here is a real world example of the data I'm working with:

Code: Select all

<LI>
	<BASICS>
		<LI_KEY>1140</LI_KEY>
		<LABEL>Allowed CAB to meet target coverage</LABEL>
	</BASICS>
	<T_DATA>
		<F_TOKENS>
			<F_TK>
				<TK_T>5</TK_T>
				<TK_TX>IF</TK_TX>
				<TK_EX_T>1</TK_EX_T>
			</F_TK>
			<F_TK>
				<TK_T>4</TK_T>
				<TK_LI_ID>1132</TK_LI_ID>
				<TK_EX_T>0</TK_EX_T>
			</F_TK>
			<F_TK>
				<TK_T>8</TK_T>
				<TK_TX>/</TK_TX>
				<TK_EX_T>6</TK_EX_T>
			</F_TK>
			<F_TK>
				<TK_T>4</TK_T>
				<TK_LI_ID>62</TK_LI_ID>
				<TK_EX_T>0</TK_EX_T>
				<TK_ANC>T</TK_ANC>
			</F_TK>
		</F_TOKENS>
	</T_DATA>
</LI>
I can pull out all the LI->BASICS->LI_KEY and LABEL values quite easily, they're in the same "basics" table in the dataset... i need to pull out all the TK_LI_ID values and their corresponding LI_KEY values, but these values are stored in two different tables bound together by the same LI parent. So in this above example my first clean data table would read:
1140, "Allowed CAB to meet target coverage"
and my 2nd data table would read:
1140, 1132
1140, 62
Last edited by Turbanator on Wed Aug 13, 2008 5:00 pm, edited 1 time in total.
Turbanator
Posts: 883
Joined: Wed Jun 08, 1983 7:00 am

Re: XML C# Question...

Post by Turbanator »

The code for pulling out LI_KEY and LABEL:

Code: Select all

            for (int i = 0; i <= myDS.Tables["Basics"].Rows.Count -1 ; i++)
            {
                dt1.Rows.Add(new object[] { myDS.Tables["Basics"].Rows[i][0].ToString(), myDS.Tables["Basics"].Rows[i][1].ToString() });
            }
sirstrongbad
Posts: 264
Joined: Tue Sep 12, 2000 7:00 am

Re: XML C# Question...

Post by sirstrongbad »

Turbanator wrote:this is a data file that holds information i need to visualize, so i'm reverse engineering it. I've got it back to an XML form, and now I need to load data off the XML into a new dataset so I can output a "clean xml" to work with for visualization.

Here is a real world example of the data I'm working with:

Code: Select all

<LI>
	<BASICS>
		<LI_KEY>1140</LI_KEY>
		<LABEL>Allowed CAB to meet target coverage</LABEL>
	</BASICS>
	<T_DATA>
		<F_TOKENS>
			<F_TK>
				<TK_T>5</TK_T>
				<TK_TX>IF</TK_TX>
				<TK_EX_T>1</TK_EX_T>
			</F_TK>
			<F_TK>
				<TK_T>4</TK_T>
				<TK_LI_ID>1132</TK_LI_ID>
				<TK_EX_T>0</TK_EX_T>
			</F_TK>
			<F_TK>
				<TK_T>8</TK_T>
				<TK_TX>/</TK_TX>
				<TK_EX_T>6</TK_EX_T>
			</F_TK>
			<F_TK>
				<TK_T>4</TK_T>
				<TK_LI_ID>62</TK_LI_ID>
				<TK_EX_T>0</TK_EX_T>
				<TK_ANC>T</TK_ANC>
			</F_TK>
		</F_TOKENS>
	</T_DATA>
</LI>
I can pull out all the LI->BASICS->LI_KEY and LABEL values quite easily, they're in the same "basics" table in the dataset... i need to pull out all the TK_LI_ID values and their corresponding LI_KEY values, but these values are stored in two different tables bound together by the same LI parent. So in this above example my first clean data table would read:
1140, "Allowed CAB to meet target coverage"
and my 2nd data table would read:
1140, 1132
1140, 62
The XML looks malformed to me... The children of F_TOKENS are all screwy. Considering those children have different keys, what happens when one of the children is not present? Defaults to null? Seems like it would make your life much easier parsing this data if the children at least matched (with no values if none are present).
Turbanator
Posts: 883
Joined: Wed Jun 08, 1983 7:00 am

Re: XML C# Question...

Post by Turbanator »

the app that builds this data (from which i pull out this XML file from), was developed in 2003... screwy is an understatement, the whole thing is fucked but it is what it is... and I need to get these relationship links out of it :\

to answer your question, the dataset reads them as blank when they're not present... so a simple check of "if ....length > 1 ... then add the item to new dt" would get rid of all the dodgy crap.
Turbanator
Posts: 883
Joined: Wed Jun 08, 1983 7:00 am

Re: XML C# Question...

Post by Turbanator »

inolen wrote:Edit: Feel free to IM me, I'm not doing much atm :)
i can't remember my icq password... lol

is my e-mail address on my icq account set to raj [at] turbanator [dot] net ?
User avatar
PhoeniX
Posts: 4067
Joined: Fri Aug 04, 2000 7:00 am

Re: XML C# Question...

Post by PhoeniX »

Turbanator wrote:
inolen wrote:Edit: Feel free to IM me, I'm not doing much atm :)
i can't remember my icq password... lol

is my e-mail address on my icq account set to raj [at] turbanator [dot] net ?
Your account isn't showing an address.. maybe it was set as hidden?
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: XML C# Question...

Post by ^misantropia^ »

Turbanator wrote:and now I need to load data off the XML into a new dataset so I can output a "clean xml" to work with for visualization.
This is the kind of work XSLT was invented for.
inolen
Posts: 705
Joined: Thu Dec 16, 1999 8:00 am

Re: XML C# Question...

Post by inolen »

^misantropia^ wrote:
Turbanator wrote:and now I need to load data off the XML into a new dataset so I can output a "clean xml" to work with for visualization.
This is the kind of work XSLT was invented for.
Yea, but then you have the overhead of learning the syntax for that :p
User avatar
MKJ
Posts: 32582
Joined: Fri Nov 24, 2000 8:00 am

Re: XML C# Question...

Post by MKJ »

xslt is easier than c#, innit
Turbanator
Posts: 883
Joined: Wed Jun 08, 1983 7:00 am

Re: XML C# Question...

Post by Turbanator »

meh... achieved it using XPath... dataset just wasn't working. XPath is slick i have to say.
Post Reply