472,958 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

retrieving distinct attributes from xml doc

In the xml document below, I would like to retrieve the distinct
attributes for the element '<Bal>'. However, I haven't had any
success. Here is what I have so far:

<TRANS>

<TRAN TRAN_DESC_CD="ACRT" TRAN_DESC="Actual Rate">

<BAL BAL_FLD_NUM="3" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="0011-01" />

</BAL>

<BAL BAL_FLD_NUM="3" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="0013-01" />

</BAL>

<BAL BAL_FLD_NUM="2" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="00112-01" />

</BAL>

<BAL BAL_FLD_NUM="6" BAL_FLD_DSCR="Actual Dispersion Amount">

<SUBDEAL SUB_Deal_ID="02342-01" />

</BAL>

</TRAN>

</TRANS>

IEnumerable<XAttributeICOA =

(from ddList in GxmlCOA.Descendants("BAL").Attributes()

select ddList).Distinct();

So the distinct bal fields should be:

1) Text = "Actual Rate Amount"

Val = 3

2) Text = "Actual Rate Amount"

Val = 2

3) Text = "Actual Dispersion Amount"

Val = 6

Unfortunately, the LINQ query isn't working.
Sep 12 '08 #1
2 4268
Test.Xml is A copy of yours

XmlDocument oXml = new XmlDocument();
oXml.Load("d:\\temp\\test.xml");
//get attribut
XmlNode testnode =
oXml.SelectSingleNode("TRANS/TRAN/BAL/SUBDEAL[@SUB_Deal_ID='00112-01']");
if (testnode==null)
{
Console.WriteLine("Not Found");
}
else
{

Console.WriteLine(testnode.Attributes[0].Value);
}
//if u want all of the tran nodes TRANS being your root
XmlNode TranNodes = oXml.SelectSingleNode("TRANS/TRAN");
for (int x = 0; x < TranNodes.ChildNodes.Count; x++)
{
//all attributes
XmlNode node = TranNodes.ChildNodes[x];
Console.WriteLine(node.Name);
if (node.Attributes.Count 0)
{
for (int y = 0; y < node.Attributes.Count; y++)
{
Console.WriteLine(node.Attributes[y].Name.ToString()
+ " " + node.Attributes[y].Value.ToString());
}
}

}
Dave

"rds80" <sh*********@gmail.comwrote in message
news:6c**********************************@b1g2000h sg.googlegroups.com...
In the xml document below, I would like to retrieve the distinct
attributes for the element '<Bal>'. However, I haven't had any
success. Here is what I have so far:

<TRANS>

<TRAN TRAN_DESC_CD="ACRT" TRAN_DESC="Actual Rate">

<BAL BAL_FLD_NUM="3" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="0011-01" />

</BAL>

<BAL BAL_FLD_NUM="3" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="0013-01" />

</BAL>

<BAL BAL_FLD_NUM="2" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="00112-01" />

</BAL>

<BAL BAL_FLD_NUM="6" BAL_FLD_DSCR="Actual Dispersion Amount">

<SUBDEAL SUB_Deal_ID="02342-01" />

</BAL>

</TRAN>

</TRANS>

IEnumerable<XAttributeICOA =

(from ddList in GxmlCOA.Descendants("BAL").Attributes()

select ddList).Distinct();

So the distinct bal fields should be:

1) Text = "Actual Rate Amount"

Val = 3

2) Text = "Actual Rate Amount"

Val = 2

3) Text = "Actual Dispersion Amount"

Val = 6

Unfortunately, the LINQ query isn't working.

Sep 12 '08 #2
Hi

You could create your own object, which overrides Equal and
GetHashCode. Something like:

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

public class DistinctAttr {
public static void Main(string[] args)
{
XElement el = XElement.Load("DistinctAttr.xml");
var attrs =
el.Descendants("BAL").
Select(e =>
new MyAttr
{
Num = e.Attribute("BAL_FLD_NUM").Value,
Dscr = e.Attribute("BAL_FLD_DSCR").Value
}
).Distinct();
foreach(var at in attrs)
{
Console.WriteLine("num: {0}, dscr: {1}", at.Num, at.Dscr);
}
}

class MyAttr
{
private string _num, _dscr;
public override bool Equals(object obj)
{
if(obj == null || !(obj is MyAttr)) return false;
MyAttr at = (MyAttr) obj;
return this._num == at.Num && this._dscr == at.Dscr;
}

public override int GetHashCode()
{
return (int) (_num.GetHashCode() + _dscr.GetHashCode());
}

public string Num {get{return this._num;} set{this._num = value;}}
public string Dscr {get{return this._dscr;} set{this._dscr =
value;}}
}
}

Regards
Steve

On Sep 12, 5:10*pm, rds80 <shah.rip...@gmail.comwrote:
In the xml document below, I would like to retrieve the distinct
attributes for the element '<Bal>'. *However, I haven't had any
success. Here is what I have so far:

<TRANS>

<TRAN TRAN_DESC_CD="ACRT" TRAN_DESC="Actual Rate">

<BAL BAL_FLD_NUM="3" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="0011-01" />

</BAL>

<BAL BAL_FLD_NUM="3" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="0013-01" />

</BAL>

<BAL BAL_FLD_NUM="2" BAL_FLD_DSCR="Actual Rate Amount">

<SUBDEAL SUB_Deal_ID="00112-01" />

</BAL>

<BAL BAL_FLD_NUM="6" BAL_FLD_DSCR="Actual Dispersion Amount">

<SUBDEAL SUB_Deal_ID="02342-01" />

</BAL>

</TRAN>

</TRANS>

IEnumerable<XAttributeICOA =

(from ddList in GxmlCOA.Descendants("BAL").Attributes()

select ddList).Distinct();

So the distinct bal fields should be:

1) Text = "Actual Rate Amount"

Val = 3

2) Text = "Actual Rate Amount"

Val = 2

3) Text = "Actual Dispersion Amount"

Val = 6

Unfortunately, the LINQ query isn't working.
Sep 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Florian | last post by:
Hi, I have a table that contains log data, usually around a million records. The table has about 10 columns with various attributes of the logged data, nothing special. We're using SQL Server...
0
by: Audun | last post by:
Hi I've been trying to read the following attributes from Active Directory: networkAddress and netbootGUID. (Using c# and the DirectoryEntry class.) Tried to search, and explicitt load these...
1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
4
by: Iain | last post by:
I've an xml document that looks a bit like this <Vendors> <Vendor Stationery="Fred" /> <Vendor Stationery="bert" /> <Vendor Stationery="bert" /> </Vendors> I want to extract a list of the...
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
4
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
1
by: Damien | last post by:
I have seen examples of selecting distinct elements by name, but my issue is that I need to get distinct attributes by name. My XML looks like: <root> <file> <rows> <row att1="1" /> <row...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.