471,330 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 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 4173
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Florian | last post: by
9 posts views Thread by Kelvin | last post: by
1 post views Thread by Damien | last post: by

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.