对于所有具有属性TranslateMe
的标签,我需要将它们转换为一些已知值。
流为:
输入文件的示例为:
<PatientLastName>Smith</PatientLastName>
<PatientDOB>1956-07-18</PatientDOB>
<PatientSSN>999999999</PatientSSN>
<Facility TranslateMe="Facility">
<Facility>TheMemorialHospital</Facility>
</Facility>
<FacilityPatientID>AAA</FacilityPatientID>
<FacilityEncounterID>BBB</FacilityEncounterID>
<Interface>
<Patient>
<FirstName>Alex</FirstName>
<MiddleInitial>Ray</MiddleInitial>
<LastName>Smith</LastName>
<DOB>1956-07-18</DOB>
<Gender TranslateMe="Gender">
<Gender>F</Gender>
</Gender>
</Patient>
</Interface>
如上所述,有两个节点需要翻译:{{1}}和Facility
。
所需的输出将是:
Gender
我将如何在XML上执行这些操作,同时牢记我将需要重构XML消息,如上所示?
到目前为止,我的过滤方式如下:
<PatientLastName>Smith</PatientLastName>
<PatientDOB>1956-07-18</PatientDOB>
<PatientSSN>999999999</PatientSSN>
<Facility>TheMemorialHospitalTRANSLATEDVALUE</Facility>
<FacilityPatientID>AAA</FacilityPatientID>
<FacilityEncounterID>BBB</FacilityEncounterID>
<Interface>
<Patient>
<FirstName>Alex</FirstName>
<MiddleInitial>Ray</MiddleInitial>
<LastName>Smith</LastName>
<DOB>1956-07-18</DOB>
<Gender>FTRANSLATEDVALUE</Gender>
</Patient>
</Interface>
答案 0 :(得分:2)
这就是我要做的。这段代码
void Main()
{
var xml = @"<root>
<PatientLastName>Smith</PatientLastName>
<PatientDOB>1956-07-18</PatientDOB>
<PatientSSN>999999999</PatientSSN>
<Facility TranslateMe=""Facility"">
<Facility>TheMemorialHospital</Facility>
</Facility>
<FacilityPatientID>AAA</FacilityPatientID>
<FacilityEncounterID>BBB</FacilityEncounterID>
<Interface>
<Patient>
<FirstName>Alex</FirstName>
<MiddleInitial>Ray</MiddleInitial>
<LastName>Smith</LastName>
<DOB>1956-07-18</DOB>
<Gender TranslateMe=""Gender"">
<Gender>F</Gender>
</Gender>
</Patient>
</Interface>
</root>";
var xdoc = XDocument.Parse(xml);
var nodes = xdoc.XPathSelectElements("//*[@TranslateMe]");
foreach(var node in nodes){
node.Attribute("TranslateMe").Remove();
node.Value = Translate(node.Value);
}
//xdoc.Dump();
}
public static string Translate(string input){
// TODO: translate input
return "TRANSLATED_VALUE";
}
产生以下输出:
<root>
<PatientLastName>Smith</PatientLastName>
<PatientDOB>1956-07-18</PatientDOB>
<PatientSSN>999999999</PatientSSN>
<Facility>TRANSLATED_VALUE</Facility>
<FacilityPatientID>AAA</FacilityPatientID>
<FacilityEncounterID>BBB</FacilityEncounterID>
<Interface>
<Patient>
<FirstName>Alex</FirstName>
<MiddleInitial>Ray</MiddleInitial>
<LastName>Smith</LastName>
<DOB>1956-07-18</DOB>
<Gender>TRANSLATED_VALUE</Gender>
</Patient>
</Interface>
</root>