2017-08-26 4 views
-2
<?xml version="1.0"?> 
<TextType IsKey="false" Name="XMLReport" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Providers 
    xmlns="Reporting"/> 
    <Sales 
     xmlns="Reporting"/> 
     <Value 
      xmlns="Reporting"> 
      <?xml version="1.0" encoding="utf-8"?> 
      <TestReport> 
       <StudyUid> 
        <![CDATA[123]]> 
       </StudyUid> 
       <Modality> 
        <![CDATA[XYZ]]> 
       </Modality> 
       <StudyDate format="DICOM">123456</StudyDate> 
       <StudyTime format="DICOM">6789</StudyTime> 
       <AccessionNumber> 
        <![CDATA[123]]> 
       </AccessionNumber> 
       <StudyDescription> 
        <![CDATA[abc def]]> 
       </StudyDescription> 
       <OperatorName format="xyz"> 
        <![CDATA[abc]]> 
       </OperatorName> 
       <PhysicianReadingStudy format="xyz"> 
        <![CDATA[^^^^]]> 
       </PhysicianReadingStudy> 
       <InstitutionName> 
        <![CDATA[xyz]]> 
       </InstitutionName> 
       <HospitalName> 
        <![CDATA[Hospital Name]]> 
       </HospitalName> 
       <ReportSet> 
        <MyReport ID="1"> 
         <ReportStatus> 
          <![CDATA[Done]]> 
         </ReportStatus> 
        </MyReport> 
        <MyReport ID="2"> 
         <ReportStatus> 
          <![CDATA[Done]]> 
         </ReportStatus> 
        </MyReport> 
        <MyReport ID="3"> 
         <ReportStatus> 
          <![CDATA[Initial]]> 
         </ReportStatus> 
        </MyReport> 
       </ReportSet> 
       <ReportImageSet /> 
       <FetusSet /> 
      </TestReport> 
     </Value> 
     <WhoSetMe xmlns="Reporting">NotSpecified 
     </WhoSetMe> 
    </TextType> 

上記のxmlをC#で解析し、MyReport/ReportSetのすべてのReportStatusに対して "ReportStatus"が "Done"であるかどうかを確認します。ここでもう一つの紆余曲折は、上記の例のようにxmlが "Value"タグで始まるxmlがもう1つ含まれていることです。ReportSetタグの下にある多くのReportStatusタグにcontainする可能性があります。誰か助けてくれますか?私はxmlの内部にxmlを持っていて、内側のxmlの内部で条件が満たされているかテストしたいと思っています。 C#ソリューションが必要です

+0

あなたがこれまでに試してみましたか?いくつかのコードを投稿できますか? – abagshaw

+0

こんにちは、私はbelow.Butような何かをしようとしています運ない – Mithun

+0

/*********************************** *******/ var value = xelement.Elements( "Value"); var reportStatus = xelement.Elements( "ReportSet")。要素( "MyReport")。選択(s => s); VAR doneReortCount = reportStatus.Where(X => x.Element( "ReportStatus")の値== "完了"。)ToListメソッド()カウント。。。 IF(reportStatus.ToList()== doneReortCount数) {// DO Somehing }/************************を****************/ – Mithun

答えて

0

あなたのXML文書は無効です。解析する前に修正する必要があります。問題は、ドキュメントには最上位の要素が1つしかないことです。あなたは2 <TextType><Providers>です。

あなたの要素のほとんどは、名前空間Reportingです。要素を参照するときに使用する必要があります。

XNamespace ns = "Reporting"; 
var value = doc.Element("Value" + ns); 

更新

ちょうどそれは、複数のXML宣言を持っているので、XML文書が無効と見なされ

XNamespace ns = "Reporting"; 
var value = xelement.Elements("Value" + ns); 

別の更新

各要素の名前空間を使用します。これを無効にする方法はありません。追加の宣言を削除するには、ドキュメントを前処理することをお勧めします。ここでは一例です(https://dotnetfiddle.net/UnuAF6

var xml = "<?xml version='1.0'?><a> <?xml version='1.0'?><b id='b' /></a>"; 
var doc = XDocument.Parse(xml.Replace(" <?xml version='1.0'?", " ")); 
var bs = doc.Descendants("b"); 
Console.WriteLine("{0} 'b' elements", bs.Count()); 
+0

TextTypeがルートノードです。 TextTypeには、子要素が含まれています。Providers、Sales、Valueなど。Value子ノードには別のXMLが含まれています。このXMLをプッシュする方法はありませんか? : – Mithun

+0

XMLの解析には、各要素の名前空間を使用する必要があります。 –

+0

しかし、解析する際に、Valueタグの要素が適切でないと不平を言っています。 。別のXMLを含むXMLがどのように解析し、バリュータグの内部XMLを取得することができますので、それがある – Mithun

0
// Can you try this? I tried to do it with LINQ to XML. 
// I assume you have multiple <TestReport /> elements in <Value /> tag 
// and var xelement is your xml variable 

// First we get all TestReport elemnts 
IEnumerable<XElement> allReports = 
    from el in xelement.Elements("TextType/Value/TestReport") 
    select el; 

// From allReports we get all MyReport elemnts 
IEnumerable<XElement> allMyReports = 
    from el in allReports.Elements("ReportSet/MyReport") 
    select el; 

// From allReports we also get all MyReport elemnts with element ReportStatus value equals "Done" 
IEnumerable<XElement> allDoneMyReports = 
    from el in allMyReports 
    where (string)el.Element("ReportStatus") == "Done" 
    select el; 

// Now we compare allMyReport with allDoneMyReports 
if (allMyReports.Count() == allDoneMyReports.Count()) 
{ 
    //DO Somehing 
} 
関連する問題