How to parse received xml into variables
|
| 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
| | Thank's! Can you provide some more details how to do this?
Regards,
S
| | 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..... /// /// Creates a new TrainingEntry DTO using the xml string that is passed /// to this method. /// /// Xml to be used when creating the DTO /// Instance of the TrainingEntry DTO public static TrainingEntry NewFromXml(string outerXml) TrainingEntry entry = new TrainingEntry();
XmlReader reader = NewReader(outerXml);
while (reader.Read()) if (reader.IsStartElement("TrainingDate", entry.TrainingDate = reader.ReadElementContentAsDateTime( "TrainingDate", Constants.DEFAULT_NAMESPACE);
if (reader.IsStartElement("TrainingNote", entry.TrainingNote = reader.ReadElementContentAsString();
if (reader.IsStartElement("StartTime", entry.StartTime = reader.ReadElementContentAsString();
if (reader.IsStartElement("EndTime", entry.EndTime = reader.ReadElementContentAsString();
if (reader.IsStartElement("WeightTrainingDetails", { entry.WeightTrainingDetails = WeightTrainingExerciseDetail.NewFromXml(reader.ReadOuterXml()); } if (reader.IsStartElement("CardioTrainingDetail", CardioTrainingExerciseDetail.AddFromXml(reader.ReadOuterXml(), entry.CardioTrainingDetail); return entry;
} Lastly, this is what the incomming XML looks like: 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"> 2006-04-12T00:00:00 test 11:30 12:30 10 12 11 12 20 10 2 test 10 0 0 25 0 1.00 200 150 130
"staeri@gmail.com" wrote:
> Thank's! Can you provide some more details how to do this? > > Regards, > S
|
|
|
|
|
 | Members Area | |
|
 | Last Web Marketing |
|
|
|
|
 | Last Programming Tips |
|
|
|
|
 | Last News |
|
|
|
|
|
|