GetElementsByTagName

M

Mel

This is my first time working with C# and XML. I have an XML file in the
format displayed at the end of this email. I need to get the individual
values out of the xml. I found GetElementsByTagName which works fine for the
unique elements such as LzAddedBy.

As you can seen near the end of the XML, there are groups of
LzAccountAllocation. I am trying to figure out the best way to code this to
group each section together. My code that I found which works is...

nodelst = doc.GetElementsByTagName("LzAccountId");
foreach (XmlNode node in nodelst)
{
LzAccountId = node.InnerText;
}

nodelst = doc.GetElementsByTagName("LzAccountName");
foreach (XmlNode node in nodelst)
{
LzAccountName = node.InnerText;
}

nodelst = doc.GetElementsByTagName("LzProposedQuantity");
foreach (XmlNode node in nodelst)
{
LzProposedQuantity = node.InnerText;
}

Is this the best way to get the individual elements? In the
LzAllocationDetail should I just spin through the elements and load them into
array?

Thanks for your help.

--------------------------------------------------------------------------------------------
<LzMessage>
- <LzOrderEvent Operation="Added" TransactionId="64764" HasMergedAccounts="N">
- <LzOrder>
- <LzOrderDetail>
- <LzAddedBy>
<Value>lzsysadmin</Value>
</LzAddedBy>
- <LzAddedDatetime>
<Value>2007120622264427</Value>
</LzAddedDatetime>
- <LzExternalOrderId>
<Value />
</LzExternalOrderId>
- <LzInstrumentBb>
<Value>KO</Value>
</LzInstrumentBb>
- <LzInstrumentCusip>
<Value>191216100</Value>
</LzInstrumentCusip>
- <LzInstrumentId>
<Value>80973.0</Value>
</LzInstrumentId>
- <LzInstrumentIsin>
<Value>US1912161007</Value>
</LzInstrumentIsin>
- <LzInstrumentName>
<Value>COCA-COLA CO COM (KO)</Value>
</LzInstrumentName>
- <LzInstrumentSedol>
<Value />
</LzInstrumentSedol>
- <LzManagerId>
<Value>mnutter</Value>
</LzManagerId>
- <LzOrderId>
<Value>O2085</Value>
</LzOrderId>
- <LzOrderPrice>
<Value>60.34</Value>
</LzOrderPrice>
- <LzSideCodes>
<Value>B</Value>
</LzSideCodes>
</LzOrderDetail>
- <LzAccountAllocation>
- <LzAllocationDetail>
- <LzAccountId>
<Value>1059</Value>
</LzAccountId>
- <LzAccountName>
<Value>1059 Wyoming State Treasurer</Value>
</LzAccountName>
- <LzProposedQuantity>
<Value>2000</Value>
</LzProposedQuantity>
</LzAllocationDetail>
</LzAccountAllocation>
- <LzAccountAllocation>
- <LzAllocationDetail>
- <LzAccountId>
<Value>1443</Value>
</LzAccountId>
- <LzAccountName>
<Value>1443 NA Convertible & Income Fund (CVT)</Value>
</LzAccountName>
- <LzProposedQuantity>
<Value>3000</Value>
</LzProposedQuantity>
</LzAllocationDetail>
</LzAccountAllocation>
- <LzAccountAllocation>
- <LzAllocationDetail>
- <LzAccountId>
<Value>1451</Value>
</LzAccountId>
- <LzAccountName>
<Value>1451 NA CONVERT & INCOME FUND 2 (CV)</Value>
</LzAccountName>
- <LzProposedQuantity>
<Value>5000</Value>
</LzProposedQuantity>
</LzAllocationDetail>
</LzAccountAllocation>
</LzOrder>
</LzOrderEvent>
</LzMessage>
 
A

Arne Vajhøj

Mel said:
This is my first time working with C# and XML. I have an XML file in the
format displayed at the end of this email. I need to get the individual
values out of the xml. I found GetElementsByTagName which works fine for the
unique elements such as LzAddedBy.

As you can seen near the end of the XML, there are groups of
LzAccountAllocation. I am trying to figure out the best way to code this to
group each section together. My code that I found which works is...

nodelst = doc.GetElementsByTagName("LzAccountId");
foreach (XmlNode node in nodelst)
{
LzAccountId = node.InnerText;
}

nodelst = doc.GetElementsByTagName("LzAccountName");
foreach (XmlNode node in nodelst)
{
LzAccountName = node.InnerText;
}

nodelst = doc.GetElementsByTagName("LzProposedQuantity");
foreach (XmlNode node in nodelst)
{
LzProposedQuantity = node.InnerText;
}

Is this the best way to get the individual elements? In the
LzAllocationDetail should I just spin through the elements and load them into
array?

Two tricks that may help you:

1) You can get a bit more control with SelectNodes than with
GetElementsByTagName.

2) You can use both on found elements (they work on other elements
than document element).

So if I have understood your problem correct then you make one select,
loop over that and for each you can select in the tree below the
found element.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top