Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading the value of an "xsi:type" attribute [unfortunate, but necessary cross-posting]

Carl Lindmark
Guest
 
Posts: n/a
#1: Nov 12 '05
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I
believe the question is better suited in this XML group*

Hello all,

I'm having some problems understanding all the ins and outs with datasets
and datatables (and navigating through the filled datatable)...

Just when I thought I had gotten the hang of it, another problem arose:
I can't seem to access the "xsi:type" attribute. That is, the XML file looks
something like this:
....
<situation>
<objectID>123</objectId>
<situationElement xsi:type="td:roadWorksType">
...
</situationElement>
</situation>

But when I try and filter the contents of the file (after I've read all
"situation" tags into a datatable) I can't seem to access the attribute
"xsi:type" - which I need to do to distinguish between different types.

When I change the XML and write "xsi_type" instead of "xsi:type", I'm able
to access this attribute by going to the right row and write
'rowname["xsi_type"]', but the files I will have to work on use the
attribute "xsi:type" and I haven't been able to access that attribute. Can
anyone help me read the value of the xsi:type attribute? Any pointers?


Thanks in advance!

Sincerely,
Carl



Martin Honnen
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Reading the value of an "xsi:type" attribute [unfortunate, but necessary cross-posting]




Carl Lindmark wrote:

[color=blue]
> Just when I thought I had gotten the hang of it, another problem arose:
> I can't seem to access the "xsi:type" attribute. That is, the XML file looks
> something like this:
> ...
> <situation>
> <objectID>123</objectId>
> <situationElement xsi:type="td:roadWorksType">[/color]

That attribute has a qualified name with the prefix xsi and the local
name type where the prefix xsi is usually bound to the namespace URI
http://www.w3.org/2001/XMLSchema-instance.
If you want to read that attribute value then you need to do
xmlElement.GetAttribute("type",
"http://www.w3.org/2001/XMLSchema-instance")


If that does not help then show your code currently reading attributes
so that we can suggest how to improve it.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Carl Lindmark
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Reading the value of an "xsi:type" attribute [unfortunate, but necessary cross-posting]



"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:Ona$z4GXFHA.3280@TK2MSFTNGP09.phx.gbl...[color=blue]
>
>
> Carl Lindmark wrote:
>
>[color=green]
>> Just when I thought I had gotten the hang of it, another problem arose:
>> I can't seem to access the "xsi:type" attribute. That is, the XML file
>> looks
>> something like this:
>> ...
>> <situation>
>> <objectID>123</objectId>
>> <situationElement xsi:type="td:roadWorksType">[/color]
>
> That attribute has a qualified name with the prefix xsi and the local name
> type where the prefix xsi is usually bound to the namespace URI
> http://www.w3.org/2001/XMLSchema-instance.
> If you want to read that attribute value then you need to do
> xmlElement.GetAttribute("type",
> "http://www.w3.org/2001/XMLSchema-instance")
>
>
> If that does not help then show your code currently reading attributes so
> that we can suggest how to improve it.
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/[/color]


Thank you very much for the reply, Martin!
If I've understood this correctly, though, the "GetAttribute()" solution is
something you use when you've created a new "XmlDocument" instance into
which you've read the XML file in question. Myself, however, I have read the
XML file into a DataSet (which I've been told to do).

If I had used the approach of reading the XML file into an XmlDocument in
the code and then stepped through the nodes, I could have used your
solution, or something like the following VB code, right?

tempNode = situation.SelectSingleNode("td:situationElement", nameSpcManager)
If Not IsNothing(tempNode) Then xsiType(i) = tempNode.Attributes(0).Value
xsiType(i) = Replace(xsiType(i), "td:", "")

But, since I've been told to do my solution with DataSet, I don't know how
I should get the xsi:type attribute value. This is the code I currently
have, but trying to read the "xsi:type" value by doing
'childRow["xsi:type"]', as I have done in the following C# code, does not
work:

DataTable myTable = myDataSet.Tables["situation"]; // get the proper table
foreach (DataRow myRow in myTable.Rows)
{
string objectId = "" + myRow["objectId"];
foreach(DataRelation myRelation in myRow.Table.ChildRelations)
{
DataRow[] childRows = myRow.GetChildRows(myRelation);
foreach (DataRow childRow in childRows)
{
if (childRow.Table.TableName.CompareTo("validityPerio d") == 0)
{
foreach (DataRow myRow2 in childRow.Table.Rows)
{
validityPeriodStart = "" + myRow2["start"];
validityPeriodEnd = "" + myRow2["end"];
}
}
if (childRow.Table.TableName.CompareTo("situationElem ent") == 0)
{
if (childRow["xsi:type"].ToString().CompareTo("td:RoadWorksType") ==
0)
{
foreach (DataRow myRow2 in childRow.Table.Rows)
{
elementObjectId = "" + myRow2["objectId"];
elementCodedDuration = "" + myRow2["codedDuration"];
}
}
}
}
}
}


Sincerely,
Carl


Carl Lindmark
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Reading the value of an "xsi:type" attribute [unfortunate, but necessary cross-posting]



"Carl Lindmark" <RepliesInNewsGroupOnly@Thanks> skrev i meddelandet
news:eSElJkHXFHA.3248@TK2MSFTNGP09.phx.gbl...[color=blue]
>
> "Martin Honnen" <mahotrash@yahoo.de> wrote in message
> news:Ona$z4GXFHA.3280@TK2MSFTNGP09.phx.gbl...[color=green]
> >
> >
> > Carl Lindmark wrote:
> >
> >[color=darkred]
> >> Just when I thought I had gotten the hang of it, another problem arose:
> >> I can't seem to access the "xsi:type" attribute. That is, the XML file
> >> looks
> >> something like this:
> >> ...
> >> <situation>
> >> <objectID>123</objectId>
> >> <situationElement xsi:type="td:roadWorksType">[/color]
> >
> > That attribute has a qualified name with the prefix xsi and the local[/color][/color]
name[color=blue][color=green]
> > type where the prefix xsi is usually bound to the namespace URI
> > http://www.w3.org/2001/XMLSchema-instance.
> > If you want to read that attribute value then you need to do
> > xmlElement.GetAttribute("type",
> > "http://www.w3.org/2001/XMLSchema-instance")
> >
> >
> > If that does not help then show your code currently reading attributes[/color][/color]
so[color=blue][color=green]
> > that we can suggest how to improve it.
> >
> > --
> >
> > Martin Honnen --- MVP XML
> > http://JavaScript.FAQTs.com/[/color]
>
>
> Thank you very much for the reply, Martin!
> If I've understood this correctly, though, the "GetAttribute()" solution[/color]
is[color=blue]
> something you use when you've created a new "XmlDocument" instance into
> which you've read the XML file in question. Myself, however, I have read[/color]
the[color=blue]
> XML file into a DataSet (which I've been told to do).
>
> If I had used the approach of reading the XML file into an XmlDocument in
> the code and then stepped through the nodes, I could have used your
> solution, or something like the following VB code, right?
>
> tempNode = situation.SelectSingleNode("td:situationElement",[/color]
nameSpcManager)[color=blue]
> If Not IsNothing(tempNode) Then xsiType(i) = tempNode.Attributes(0).Value
> xsiType(i) = Replace(xsiType(i), "td:", "")
>
> But, since I've been told to do my solution with DataSet, I don't know[/color]
how[color=blue]
> I should get the xsi:type attribute value. This is the code I currently
> have, but trying to read the "xsi:type" value by doing
> 'childRow["xsi:type"]', as I have done in the following C# code, does not
> work:
>
> DataTable myTable = myDataSet.Tables["situation"]; // get the proper[/color]
table[color=blue]
> foreach (DataRow myRow in myTable.Rows)
> {
> string objectId = "" + myRow["objectId"];
> foreach(DataRelation myRelation in myRow.Table.ChildRelations)
> {
> DataRow[] childRows = myRow.GetChildRows(myRelation);
> foreach (DataRow childRow in childRows)
> {
> if (childRow.Table.TableName.CompareTo("validityPerio d") == 0)
> {
> foreach (DataRow myRow2 in childRow.Table.Rows)
> {
> validityPeriodStart = "" + myRow2["start"];
> validityPeriodEnd = "" + myRow2["end"];
> }
> }
> if (childRow.Table.TableName.CompareTo("situationElem ent") == 0)
> {
> if (childRow["xsi:type"].ToString().CompareTo("td:RoadWorksType")[/color]
==[color=blue]
> 0)
> {
> foreach (DataRow myRow2 in childRow.Table.Rows)
> {
> elementObjectId = "" + myRow2["objectId"];
> elementCodedDuration = "" + myRow2["codedDuration"];
> }
> }
> }
> }
> }
> }
>
>
> Sincerely,
> Carl[/color]



I know the code looks a bit complicated (and maybe not totally correct?),
but I'm not COMPLETELY off the mark, now am I? One IS supposed to be able
to work with datasets & tables this way, isn't one? And shouldn't one also
be able to read the value of attributes as I described?

Any input would be greatly appreciated!
/Carl


Closed Thread