Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read the data from xml string so how to read it while it contains multiple same name node in head node any suggestion how can i do it may be some time it will be more and some time it will be one or none so which is best approach to adopt here is my sample xml
XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetPatientFullMedicationHistoryResponse xmlns="https://secure.newcropaccounts.com/V7/webservices">
<GetPatientFullMedicationHistoryResult>
<result>
<Status>OK</Status>
<Message />
<XmlResponse />
<RowCount>2</RowCount>
<Timing>0</Timing>
</result>
<patientDrugDetail>

<PatientDrugDetail>
<ExternalPatientID>166</ExternalPatientID>
<DrugID>183716</DrugID>
<DrugTypeID>F</DrugTypeID>
<DrugName>Pondimin</DrugName>
<Strength>20</Strength>
<StrengthUOM>mg</StrengthUOM>
</patientDrugDetail>

<PatientDrugDetail>
<ExternalPatientID>166</ExternalPatientID>
<DrugID>183716</DrugID>
<DrugTypeID>F</DrugTypeID>
<DrugName>Pondimin</DrugName>
<Strength>20</Strength>
<StrengthUOM>mg</StrengthUOM>
</PatientDrugDetail>

</patientDrugDetail>

</GetPatientFullMedicationHistoryResult>
</GetPatientFullMedicationHistoryResponse>
</soap:Body>
</soap:Envelope>
Posted
Updated 1-Apr-14 4:05am
v2
Comments
[no name] 31-Mar-14 11:58am    
Should that be <PatientDrugDetails> for the first patientDrugDetail element?
Jawad Ahmed Tanoli 31-Mar-14 12:09pm    
yes it will be like this <patientDrugDetail> <PatientDrugDetail>some data tags</patientDrugDetail><PatientDrugDetail>some data tags</patientDrugDetail> </patientDrugDetail>
Jawad Ahmed Tanoli 31-Mar-14 12:10pm    
there is a another node rowcount which will tell how many tags of <PatientDrugDetail>

Jawad,

I am not sure why you are getting nothing back. But try the below which is returning me 2 Patient drug detail records.

XML
string str = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <soap:Body> <GetPatientFullMedicationHistoryResponse xmlns=""https://secure.newcropaccounts.com/V7/webservices""> <GetPatientFullMedicationHistoryResult> <result> <Status>OK</Status> <Message /> <XmlResponse /> <RowCount>2</RowCount> <Timing>0</Timing> </result> <PatientDrugDetails>  <PatientDrugDetail> <ExternalPatientID>166</ExternalPatientID> <DrugID>183716</DrugID> <DrugTypeID>F</DrugTypeID> <DrugName>Pondimin</DrugName> <Strength>20</Strength> <StrengthUOM>mg</StrengthUOM> </PatientDrugDetail>  <PatientDrugDetail> <ExternalPatientID>166</ExternalPatientID> <DrugID>183716</DrugID> <DrugTypeID>F</DrugTypeID> <DrugName>Pondimin</DrugName> <Strength>20</Strength> <StrengthUOM>mg</StrengthUOM> </PatientDrugDetail>  </PatientDrugDetails>  </GetPatientFullMedicationHistoryResult> </GetPatientFullMedicationHistoryResponse> </soap:Body> </soap:Envelope>";
C#
XNamespace ns = "https://secure.newcropaccounts.com/V7/webservices";
XDocument doc = XDocument.Parse(str);
IEnumerable<XElement> list1 = doc.Descendants(ns+"PatientDrugDetail");


OR you could use LinQ as in Solution 1

C#
XNamespace ns = "https://secure.newcropaccounts.com/V7/webservices";
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";

XDocument doc = XDocument.Parse(str);
XElement root = doc.Root.Element(soap+"Body").Element(ns+"GetPatientFullMedicationHistoryResponse").Element(ns+"GetPatientFullMedicationHistoryResult").Element(ns+"PatientDrugDetails");
            
IEnumerable<XElement> patDrugDet = from el in root.Elements(ns + "PatientDrugDetail") select el;
 
Share this answer
 
v3
Comments
Jawad Ahmed Tanoli 2-Apr-14 8:10am    
Thanks for briefly reply :)
now what is the best way to read from list1 all nodes ..
Jawad Ahmed Tanoli 2-Apr-14 10:22am    
I am doing in this way to read..
foreach (var Xval in list1){
String DrugName= Xval.Elements("{https://secure.newcropaccounts.com/V7/webservices}DrugName").Nodes().Single().ToString();
}
Guruprasad.K.Basavaraju 2-Apr-14 11:10am    
Below are two options.

Option 1:

foreach (XElement xE in list1)
{
string DrugName = xE.Element(ns+"DrugName").Value.ToString();
}

Option 2:

string[] drugs = (from el in root.Elements(ns + "PatientDrugDetail").Elements(ns+"DrugName") select el.Value.ToString()).ToArray();
Using Linq you could use

XML
IEnumerable<XElement> patDrugDet=
    from el in root.Elements("PatientDrugDetail")
    select el;
 
Share this answer
 
Comments
Jawad Ahmed Tanoli 31-Mar-14 12:45pm    
what is root ?
this xml is in string variable.do i need to convert it into some other data type like xml?
Guruprasad.K.Basavaraju 31-Mar-14 13:20pm    
string str = @"YOUR_XML_STRING";
XElement root= XElement.Parse(str);
Jawad Ahmed Tanoli 1-Apr-14 10:09am    
it return nothing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900