Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read XML .Net

Posted on 2010-09-21
20
Medium Priority
?
631 Views
Last Modified: 2012-05-10
I want to read values from an xml as this :


XXXXXXXXXXXXXXXX
PPPPPPPPPPPPPPPPPPPP
MMMMMMMMMMMMM
NNNNNNNNNNNNNNNNNN
YYYYYYYYYYYYYY
ZZZZZZZZZZZZZZZZZ

values
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<soapenv:Header>
<wsa:Address>
http://www.w3.org/2005/08/addressing/none
</wsa:Address>
></wsa:ReplyTo></soapenv:Header><soapenv:Body>
<EnergyAccountReport DtdRelease="1" DtdVersion="1">
<DocumentIdentification v="ZZZZZZZZZZZZZZZZZZZZZ" />
<DocumentVersion v="1" />
<DocumentType v="A11" />
<DocumentStatus v="A02" />
<ProcessType v="A05" />
<ClassificationType v="A01" />
<SenderIdentification codingScheme="A01" v="NNNNNNNNNNNNNNNNN" />
<SenderRole v="A09" />
<ReceiverIdentification codingScheme="A01" v="MMMMMMMMMMMMMMMMM" />
<ReceiverRole v="A05" />
<DocumentDateTime v="2010-09-21T12:39Z" />
<AccountingPeriod v="2010-08-10T00:00Z/2010-08-10T23:59Z" />
<Domain codingScheme="A01" v="PPPPPPPPPPPPPPPPPPPPP" />
<AccountTimeSeries>
<SendersTimeSeriesIdentification v="00000000000000000000000000000000001" />
<BusinessType v="A14" />
<Product v="8716867000030" />
<ObjectAggregation v="A02" />
<Area codingScheme="A01" v="10YTR-1001A0006O" /> 



<Party xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" /><AgreementIdentification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" /><MeasurementUnit v="MWH" /><Currency v="TRY" /><AccountingPoint codingScheme="A01" v="XXXXXXXXXXXXX" /><Period>
<TimeInterval v="2010-08-10T00:00Z/2010-08-10T23:59Z" /><Resolution v="PT1H" /><AccountInterval>
<Pos v="1" /><InQty v="0.0" /><OutQty v="YYYYYYYYYYY" /><SettlementAmount v="0.0" /></AccountInterval><AccountInterval>
<Pos v="2" /><InQty v="0.0" /><OutQty v="YYYYYYYYYYY" /><SettlementAmount v="0.0" /></AccountInterval><AccountInterval>


...........



<Party xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" /><AgreementIdentification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" /><MeasurementUnit v="MWH" /><Currency v="TRY" /><AccountingPoint codingScheme="A01" v="XXXXXXXXXXXXXXXXX" /><Period>
<TimeInterval v="2010-08-10T00:00Z/2010-08-10T23:59Z" /><Resolution v="PT1H" /><AccountInterval>
<Pos v="1" /><InQty v="0.0" /><OutQty v="YYYYYYYYYYYYY" /><SettlementAmount v="0.0" /></AccountInterval><AccountInterval>
<Pos v="2" /><InQty v="0.0" /><OutQty v="YYYYYYYYYYYYY" /><SettlementAmount v="0.0" /></AccountInterval><AccountInterval>




................

Open in new window

0
Comment
Question by:3XLcom
  • 11
  • 9
20 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33723942
There are several ways to do it:
1. Read the entire xml in a dataset, and use the right table, columns to get your data

2. Use xpath-querys

Can you give your complete xml-file, so that I can test both ways. Also tell me exactly what you need.

For example:
XXXXXXXXXXXXXXXX                   --> xml-tag:
PPPPPPPPPPPPPPPPPPPP
MMMMMMMMMMMMM                   --> xml-tag: ReceiverIdentification , attribute v
NNNNNNNNNNNNNNNNNN            --> xml-tag: SenderIdentification , attribute v
YYYYYYYYYYYYYY
ZZZZZZZZZZZZZZZZZ

'load xml into a dataset to use here
Dim dSet As New DataSet
Dim fs As FileStream


'open the xml file so we can use it to fill the dataset
Try
fs = New FileStream("C:\someXMLfile.xml, FileMode.Open)
Catch e As Exception
Debug.WriteLine(e)
End Try


'fill the dataset
Try
dSet.ReadXml(fs)
Catch ex As Exception
Debug.WriteLine(ex)
Finally
fs.Close()
End Try

Open in new window

0
 

Author Comment

by:3XLcom
ID: 33724024
real xml file is tooooo long
:(
unfortunately so could you send me the code so i could test it
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33724046
You can add the xml-file into the question, you don't have to copy and paste it...
Without correct xml it's very hard to give you a good programming solution.

Did you try the dataset readxml ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:3XLcom
ID: 33724091
unfortunatelly there are some important values in it so i need to try on this side for to check if it is work :( but if you help me i will be very glad for this
this is the main part of xml so you could also complete this part of xml to test
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33724159
Can you tell me the structure than, before I test further. Is this part repeating (for different party's ?) Something like below ?

<Party xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" /><AgreementIdentification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" /><MeasurementUnit v="MWH" /><Currency v="TRY" /><AccountingPoint codingScheme="A01" v="XXXXXXXXXXXXX" /><Period>
<TimeInterval v="2010-08-10T00:00Z/2010-08-10T23:59Z" /><Resolution v="PT1H" /><AccountInterval>
<Pos v="1" /><InQty v="0.0" /><OutQty v="YYYYYYYYYYY" /><SettlementAmount v="0.0" /></AccountInterval><AccountInterval>
<Pos v="2" /><InQty v="0.0" /><OutQty v="YYYYYYYYYYY" /><SettlementAmount v="0.0" /></AccountInterval><AccountInterval>

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
	<soapenv:Header>
		<wsa:Address>
			http://www.w3.org/2005/08/addressing/none
		</wsa:Address>
	</soapenv:Header>
	<soapenv:Body>
		<EnergyAccountReport DtdRelease="1" DtdVersion="1">
			<DocumentIdentification v="ZZZZZZZZZZZZZZZZZZZZZ" />
			<DocumentVersion v="1" />
			<DocumentType v="A11" />
			<DocumentStatus v="A02" />
			<ProcessType v="A05" />
			<ClassificationType v="A01" />
			<SenderIdentification codingScheme="A01" v="NNNNNNNNNNNNNNNNN" />
			<SenderRole v="A09" />
			<ReceiverIdentification codingScheme="A01" v="MMMMMMMMMMMMMMMMM" />
			<ReceiverRole v="A05" />
			<DocumentDateTime v="2010-09-21T12:39Z" />
			<AccountingPeriod v="2010-08-10T00:00Z/2010-08-10T23:59Z" />
			<Domain codingScheme="A01" v="PPPPPPPPPPPPPPPPPPPPP" />
			<AccountTimeSeries>
				<SendersTimeSeriesIdentification v="00000000000000000000000000000000001" />
				<BusinessType v="A14" />
				<Product v="8716867000030" />
				<ObjectAggregation v="A02" />
				<Area codingScheme="A01" v="10YTR-1001A0006O" />
			</AccountTimeSeries>
			<partys>
				<partyOne>
					<Party xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
					<AgreementIdentification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
					<MeasurementUnit v="MWH" />
					<Currency v="TRY" />
					<AccountingPoint codingScheme="A01" v="XXXXXXXXXXXXX" />
					<Period>
						<TimeInterval v="2010-08-10T00:00Z/2010-08-10T23:59Z" />
						<Resolution v="PT1H" />
						<AccountInterval>
							<Pos v="1" />
							<InQty v="0.0" />
							<OutQty v="YYYYYYYYYYY" />
							<SettlementAmount v="0.0" />
						</AccountInterval>
						<AccountInterval>
							<Pos v="2" />
							<InQty v="0.0" />
							<OutQty v="YYYYYYYYYYY" />
							<SettlementAmount v="0.0" />
						</AccountInterval>
					</Period>
				</partyOne>
				<partyOne>
					<Party xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
					<AgreementIdentification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
					<MeasurementUnit v="MWH" />
					<Currency v="TRY" />
					<AccountingPoint codingScheme="A01" v="XXXXXXXXXXXXX" />
					<Period>
						<TimeInterval v="2010-08-10T00:00Z/2010-08-10T23:59Z" />
						<Resolution v="PT1H" />
						<AccountInterval>
							<Pos v="1" />
							<InQty v="0.0" />
							<OutQty v="YYYYYYYYYYY" />
							<SettlementAmount v="0.0" />
						</AccountInterval>
						<AccountInterval>
							<Pos v="2" />
							<InQty v="0.0" />
							<OutQty v="YYYYYYYYYYY" />
							<SettlementAmount v="0.0" />
						</AccountInterval>
					</Period>
				</partyOne>
			</partys>
		
		</EnergyAccountReport>
	</soapenv:Body>
</soapenv:Envelope>

Open in new window

0
 

Author Comment

by:3XLcom
ID: 33724184
exactly yes
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33724227
Tell me exactly what you need. I assume also that you want these items for each party ?

For example:
XXXXXXXXXXXXXXXX                   --> xml-tag:
PPPPPPPPPPPPPPPPPPPP
MMMMMMMMMMMMM                   --> xml-tag: ReceiverIdentification , attribute v
NNNNNNNNNNNNNNNNNN            --> xml-tag: SenderIdentification , attribute v
YYYYYYYYYYYYYY
ZZZZZZZZZZZZZZZZZ

0
 

Author Comment

by:3XLcom
ID: 33724236
yes
0
 

Author Comment

by:3XLcom
ID: 33724357
that is what totaly i need as you write this parts of xml
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33724377
>> that is what totaly i need as you write this parts of xml

So you don't have figured out what mapping you need ? Which field of the xml you need ?
0
 

Author Comment

by:3XLcom
ID: 33724450
sir this xxxxx yyyy zzzzz in reality has some values i need to read them that is all
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 33724533
you can try the following (with the xml from above, it works)
    Public Class FromXML
        Public xxx As String
        Public yyy As String
        Public mmm As String
        Public nnn As String
        Public zzz As String
    End Class
    Private Sub testXML()
        Dim doc As XDocument

        doc = XDocument.Load("c:\test.xml")

        Dim myList As List(Of FromXML) = New List(Of FromXML)

        For Each xParty As XElement In doc.Descendants("Party")

            For Each xAccount As XElement In xParty.Parent().Descendants("AccountInterval")
                Dim x As FromXML = New FromXML()
                ' as far as I see it, these items are equal for all objects
                x.zzz = doc.Descendants("DocumentIdentification").ElementAt(0).Attribute("v").Value
                x.nnn = doc.Descendants("SenderIdentification").ElementAt(0).Attribute("v").Value
                x.mmm = doc.Descendants("ReceiverIdentification").ElementAt(0).Attribute("v").Value
                ' this items is for each party
                x.xxx = xParty.Parent().Descendants("AccountingPoint").ElementAt(0).Attribute("v").Value
                ' this item is for each party, for each accountinterval
                x.yyy = xAccount.Descendants("OutQty").ElementAt(0).Attribute("v").Value
                myList.Add(x)
            Next
        Next

    End Sub

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33724544
>> sir this xxxxx yyyy zzzzz in reality has some values i need to read them that is all

I agree, but where exactly in the xml are the coming from ? From the fields I guessed above in the code ?
0
 

Author Comment

by:3XLcom
ID: 33724854
that code giving error :
        For Each xParty As XmlElement In doc.Descendants("Party")
0
 

Author Comment

by:3XLcom
ID: 33724860
because of this :
Descendants
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33724906
That's possible... Probably the layout of the xml is not completly the same as I used !
So I had to guess how the xml is structured (and I used it like this:

<soapenv:Body>
   <EnergyAccountReport>
      <Partys>
         <PartyOne>
             <Party> .... </Party>
             <Party> .... </Party>
         </PartyOne>
      </Partys>
   </EnergyAccountReport>
</soapenv:Body>
0
 

Author Comment

by:3XLcom
ID: 33724920
no no .net does not accept the word :
Descendants
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33724953
What version of .net are you using ? I used 3.5 (with LINQ)
0
 

Author Comment

by:3XLcom
ID: 33724990
vs2010 :D but could not find linq on the reference list let me check it on .net 4 framework
0
 

Author Closing Comment

by:3XLcom
ID: 33725037
Worked perfectly thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question