468,268 Members | 1,713 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

How to parse received xml into variables

I'm using the following code to receive information from a web service:

Dim o As New MyWebServiceProxyClient.DataImport
Dim r As New MyWebServiceProxyClient.DataImportCompanyResult

r = o.DataImportCompany("login1", "login2", "pwd", "lang", "package",
"searchno")

r contains FieldNames and FieldValues. How can I loop through r and put
the FieldNames and FieldValues into variables?

Regards,

S

May 10 '06 #1
3 1129
If r is xml you can load the xml into an xml reader, then use the reader to
go to each element, get the value from the element and assign it into a
variable.

"st****@gmail.com" wrote:
I'm using the following code to receive information from a web service:

Dim o As New MyWebServiceProxyClient.DataImport
Dim r As New MyWebServiceProxyClient.DataImportCompanyResult

r = o.DataImportCompany("login1", "login2", "pwd", "lang", "package",
"searchno")

r contains FieldNames and FieldValues. How can I loop through r and put
the FieldNames and FieldValues into variables?

Regards,

S

May 10 '06 #2
Thank's! Can you provide some more details how to do this?

Regards,

S

May 10 '06 #3
The 2 code snippets below show how to create an xml reader in the web method.
The second snippet shows how to use a static method named NewFromXml on a
Data Transfer Object. The newFromXml method uses the reader to poupulate
itself.

Snippet 1, parse the incomming data:
[WebMethod(Description="Creates a training entry and its associated
child records." +
"Consumers of this method should invoke this method using a
predifined info path form." +
"The value passed in should be an xml document. The xml document
will be transformed " +
"into a DTO then into an Entity and then persisted to the database.")]
public int CreateTrainingEntry(string value)
{
Debug.WriteLine(value);

StringReader inputStream = new StringReader(value);
XmlTextReader textReader = new XmlTextReader(inputStream);

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
settings.CloseInput = true;
XmlReader reader = XmlReader.Create(textReader, settings);

TrainingEntry tmpEntry = null;
while (reader.Read())
{
if (reader.IsStartElement("newTrainingEntry",
Constants.DEFAULT_NAMESPACE))
{
tmpEntry = TrainingEntry.NewFromXml(reader.ReadOuterXml());
}
}

int newId = -1;
if (tmpEntry != null)
{
AdminManager manager = new AdminManager();
manager.CreateTrainingEntry(tmpEntry,out newId);
}

return newId;
}
Snippet 2, demonstrates the NewFromXml method.....
/// <summary>
/// Creates a new TrainingEntry DTO using the xml string that is
passed
/// to this method.
/// </summary>
/// <param name="outerXml">Xml to be used when creating the
DTO</param>
/// <returns>Instance of the TrainingEntry DTO</returns>
public static TrainingEntry NewFromXml(string outerXml)
{
TrainingEntry entry = new TrainingEntry();

XmlReader reader = NewReader(outerXml);

while (reader.Read())
{
if (reader.IsStartElement("TrainingDate",
Constants.DEFAULT_NAMESPACE))
entry.TrainingDate = reader.ReadElementContentAsDateTime(
"TrainingDate", Constants.DEFAULT_NAMESPACE);

if (reader.IsStartElement("TrainingNote",
Constants.DEFAULT_NAMESPACE))
entry.TrainingNote = reader.ReadElementContentAsString();

if (reader.IsStartElement("StartTime",
Constants.DEFAULT_NAMESPACE))
entry.StartTime = reader.ReadElementContentAsString();

if (reader.IsStartElement("EndTime",
Constants.DEFAULT_NAMESPACE))
entry.EndTime = reader.ReadElementContentAsString();

if (reader.IsStartElement("WeightTrainingDetails",
Constants.DEFAULT_NAMESPACE))
{
entry.WeightTrainingDetails =
WeightTrainingExerciseDetail.NewFromXml(reader.Rea dOuterXml());
}

if (reader.IsStartElement("CardioTrainingDetail",
Constants.DEFAULT_NAMESPACE))
{

CardioTrainingExerciseDetail.AddFromXml(reader.Rea dOuterXml(),
entry.CardioTrainingDetail);
}
}
return entry;

}
Lastly, this is what the incomming XML looks like:
<tns:NewTrainingEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://tempuri.org/"
xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:ns1="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-13T04:12:38"
xmlns:ns2="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-13T04:24:03"
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-13T03:32:17" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003">
<tns:newTrainingEntry>
<tns:TrainingDate>2006-04-12T00:00:00</tns:TrainingDate>
<tns:TrainingNote>test</tns:TrainingNote>
<tns:StartTime>11:30</tns:StartTime>
<tns:EndTime>12:30</tns:EndTime>
<tns:WeightTrainingDetails>
<tns:Sets>
<tns:Set>
<tns:Weight>10</tns:Weight>
<tns:Reps>12</tns:Reps>
</tns:Set>
<tns:Set>
<tns:Weight>11</tns:Weight>
<tns:Reps>12</tns:Reps>
</tns:Set>
<tns:Set>
<tns:Weight>12</tns:Weight>
<tns:Reps>12</tns:Reps>
</tns:Set>
<tns:Set>
<tns:Weight>20</tns:Weight>
<tns:Reps>10</tns:Reps>
</tns:Set>
</tns:Sets>
<tns:ExerciseId>2</tns:ExerciseId>
<tns:ExerciseNote>test</tns:ExerciseNote>
</tns:WeightTrainingDetails>
<tns:CardioTrainingDetail>
<tns:CardioTrainingExerciseDetail>
<tns:ExerciseId>10</tns:ExerciseId>
<tns:Duration>
<tns:Seconds>0</tns:Seconds>
<tns:Hours>0</tns:Hours>
<tns:Minuites>25</tns:Minuites>
<tns:Days>0</tns:Days>
</tns:Duration>
<tns:Distance>1.00</tns:Distance>
<tns:Calories>200</tns:Calories>
<tns:TargetHeartRate>150</tns:TargetHeartRate>
<tns:AverageHeartRate>130</tns:AverageHeartRate>
<tns:TimeInTargetZone>
<tns:Seconds>0</tns:Seconds>
<tns:Hours>0</tns:Hours>
<tns:Minuites>25</tns:Minuites>
<tns:Days>0</tns:Days>
</tns:TimeInTargetZone>
</tns:CardioTrainingExerciseDetail>
</tns:CardioTrainingDetail>
</tns:newTrainingEntry>
</tns:NewTrainingEntry>

"st****@gmail.com" wrote:
Thank's! Can you provide some more details how to do this?

Regards,

S

May 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John A. Irwin | last post: by
2 posts views Thread by Andrew Ayers | last post: by
5 posts views Thread by John Hanley | last post: by
reply views Thread by Klaus Bonadt | last post: by
6 posts views Thread by Ian Davies | last post: by
AdrianH
1 post views Thread by AdrianH | last post: by
4 posts views Thread by ShutterMan | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.