如何从c#中的多个相同节点xml读取数据 [英] how to read data from multiple same node xml in c#

查看:1078
本文介绍了如何从c#中的多个相同节点xml读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从xml字符串中读取数据,以便在头节点中包含多个同名节点时如何读取它的任何建议我怎么能这样做可能有一段时间它会更多和一些时间它将是一个或没有所以这是采用这里最好的方法是我的样本xml

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

推荐答案

使用Linq你可以使用



Using Linq you could use

IEnumerable<XElement> patDrugDet=
    from el in root.Elements("PatientDrugDetail")
    select el;


Jawad,



我不知道为什么你什么都没有回来。但请尝试下面的回复2患者药物详细记录。



Jawad,

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

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>";

XNamespace ns = "https://secure.newcropaccounts.com/V7/webservices";
XDocument doc = XDocument.Parse(str);
IEnumerable<XElement> list1 = doc.Descendants(ns+"PatientDrugDetail");





或者您可以在解决方案1中使用LinQ





OR you could use LinQ as in Solution 1

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;


这篇关于如何从c#中的多个相同节点xml读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆