473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 4299
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.