• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 640
  • Last Modified:

Read XML .Net

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
3XLcom
Asked:
3XLcom
  • 11
  • 9
1 Solution
 
DhaestCommented:
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
 
3XLcomAuthor Commented:
real xml file is tooooo long
:(
unfortunately so could you send me the code so i could test it
0
 
DhaestCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
3XLcomAuthor Commented:
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
 
DhaestCommented:
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
 
3XLcomAuthor Commented:
exactly yes
0
 
DhaestCommented:
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
 
3XLcomAuthor Commented:
yes
0
 
3XLcomAuthor Commented:
that is what totaly i need as you write this parts of xml
0
 
DhaestCommented:
>> 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
 
3XLcomAuthor Commented:
sir this xxxxx yyyy zzzzz in reality has some values i need to read them that is all
0
 
DhaestCommented:
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
 
DhaestCommented:
>> 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
 
3XLcomAuthor Commented:
that code giving error :
        For Each xParty As XmlElement In doc.Descendants("Party")
0
 
3XLcomAuthor Commented:
because of this :
Descendants
0
 
DhaestCommented:
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
 
3XLcomAuthor Commented:
no no .net does not accept the word :
Descendants
0
 
DhaestCommented:
What version of .net are you using ? I used 3.5 (with LINQ)
0
 
3XLcomAuthor Commented:
vs2010 :D but could not find linq on the reference list let me check it on .net 4 framework
0
 
3XLcomAuthor Commented:
Worked perfectly thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now