Solved

Read XML .Net

Posted on 2010-09-21
20
607 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now