我在xml里面有一个xml,想要测试一个条件是否符合内部xml。需要C#解决方案
问题描述:
<?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>
我想解析C#中的上述xml,并检查MyReport/ReportSet下的所有ReportStatus是否为“完成”ReportStatus。另外一个扭曲的地方是xml包含一个以上的例子中的“Value”标签开始的xml。它可以在ReportSet标签下包含许多ReportStatus标签。有人可以帮帮我吗?我在xml里面有一个xml,想要测试一个条件是否符合内部xml。需要C#解决方案
答
您的XML文档无效。在尝试解析它之前,您需要修复它。问题是文档只能有一个顶层元素;你有2 <TextType>
和<Providers>
。
大多数元素是名称空间Reporting
。您需要在引用元素时使用它。
XNamespace ns = "Reporting";
var value = doc.Element("Value" + ns);
更新
只需使用的命名空间的每个元素
XNamespace ns = "Reporting";
var value = xelement.Elements("Value" + ns);
另一个更新
的XML文档被认为是无效的,因为它有多个XML声明;无法禁用此功能。我建议您预先处理文档以删除额外的声明。下面是一个例子(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());
答
// 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
}
你尝试过这么远吗?你可以发布一些代码吗? – abagshaw
嗨,我正在尝试下面的东西。但没有运气 – Mithun
/*********************************** *******/ var value = xelement.Elements(“Value”); var reportStatus = xelement.Elements(“ReportSet”)。Elements(“MyReport”)。Select(s => s); var doneReortCount = reportStatus.Where(x => x.Element(“ReportStatus”)。Value ==“Done”)。ToList()。Count; (reportStatus.ToList()。Count == doneReortCount) { // DO do Somehing } /************************ ****************/ – Mithun