繁体   English   中英

在特定父节点下添加新节点

[英]Adding new node under a specific parent node

一直在研究 Linq 到 XML,到目前为止我已经能够生成以下 XML:

<?xml version="1.0" encoding="utf-8"?>
<_OCROUTPUT xmlns="http://whatever.com/output.xsd">
    <_Payer>
        <_Name>_Name1</_Name>
        <_Address1>_Address11</_Address1>
        <_Address2>_Address21</_Address2>
        <_City>_City1</_City>
        <_State>_State1</_State>
        <_ZipCode>_ZipCode1</_ZipCode>
    </_Payer>
    <_SPANNINGSERVICELINES>
        <_ServiceDate>_ServiceDate1</_ServiceDate>
        <_CptCode>_CptCode1</_CptCode>
        <_Modifier>_Modifier1</_Modifier>
        <_ServiceUnits>_ServiceUnits1</_ServiceUnits>
        <_ServiceCharges>1</_ServiceCharges>
        <_AllowedAmount>1</_AllowedAmount>
        <_NonCoveredAmount>_NonCoveredAmount1</_NonCoveredAmount>
        <_DeniedAmount>1</_DeniedAmount>
        <_ReasonCode>_ReasonCode1</_ReasonCode>
        <_PaymentAmount>1</_PaymentAmount>
        <_ContractAllowance>1</_ContractAllowance>
        <_Sequestration>1</_Sequestration>
        <_Deductible>1</_Deductible>
        <_Copay>1</_Copay>
        <_CoInsurance>1</_CoInsurance>
        <_PrimaryPayerPayment>1</_PrimaryPayerPayment>
    </_SPANNINGSERVICELINES>    
    <_PATIENTDETAILS>
        <_PatientAccNo>_PatientAccNo1</_PatientAccNo>
        <_PatientName>_PatientName1</_PatientName>
        <_HicNo>_HicNo1</_HicNo>
        <_MedRecNo>_MedRecNo1</_MedRecNo>
        <_SERVICELINES>
            <_ServiceDate>_ServiceDate1</_ServiceDate>
            <_CptCode>_CptCode1</_CptCode>
            <_Modifier>_Modifier1</_Modifier>
            <_ServiceUnits>_ServiceUnits1</_ServiceUnits>
            <_ServiceCharges>1</_ServiceCharges>
            <_AllowedAmount>1</_AllowedAmount>
            <_NonCoveredAmount>_NonCoveredAmount1</_NonCoveredAmount>
            <_DeniedAmount>1</_DeniedAmount>
            <_ReasonCode>_ReasonCode1</_ReasonCode>
            <_PaymentAmount>1</_PaymentAmount>
            <_ContractAllowance>1</_ContractAllowance>
            <_Sequestration>1</_Sequestration>
            <_Deductible>1</_Deductible>
            <_Copay>1</_Copay>
            <_CoInsurance>1</_CoInsurance>
            <_PrimaryPayerPayment>1</_PrimaryPayerPayment>
        </_SERVICELINES>        
    </_PATIENTDETAILS>
    <_PATIENTDETAILS>
        <_PatientAccNo>_PatientAccNo2</_PatientAccNo>
        <_PatientName>_PatientName2</_PatientName>
        <_HicNo>_HicNo2</_HicNo>
        <_MedRecNo>_MedRecNo2</_MedRecNo>
        <_SERVICELINES>
            <_ServiceDate>_ServiceDate4</_ServiceDate>
            <_CptCode>_CptCode4</_CptCode>
            <_Modifier>_Modifier4</_Modifier>
            <_ServiceUnits>_ServiceUnits4</_ServiceUnits>
            <_ServiceCharges>0.9</_ServiceCharges>
            <_AllowedAmount>0.9</_AllowedAmount>
            <_NonCoveredAmount>_NonCoveredAmount4</_NonCoveredAmount>
            <_DeniedAmount>0.9</_DeniedAmount>
            <_ReasonCode>_ReasonCode4</_ReasonCode>
            <_PaymentAmount>0.9</_PaymentAmount>
            <_ContractAllowance>0.9</_ContractAllowance>
            <_Sequestration>0.9</_Sequestration>
            <_Deductible>0.9</_Deductible>
            <_Copay>0.9</_Copay>
            <_CoInsurance>0.9</_CoInsurance>
            <_PrimaryPayerPayment>0.9</_PrimaryPayerPayment>
        </_SERVICELINES>        
    </_PATIENTDETAILS>
    <_PATIENTDETAILS>
        <_PatientAccNo>_PatientAccNo3</_PatientAccNo>
        <_PatientName>_PatientName3</_PatientName>
        <_HicNo>_HicNo3</_HicNo>
        <_MedRecNo>_MedRecNo3</_MedRecNo>
        <_SERVICELINES>
            <_ServiceDate>_ServiceDate7</_ServiceDate>
            <_CptCode>_CptCode7</_CptCode>
            <_Modifier>_Modifier7</_Modifier>
            <_ServiceUnits>_ServiceUnits7</_ServiceUnits>
            <_ServiceCharges>50.5</_ServiceCharges>
            <_AllowedAmount>50.5</_AllowedAmount>
            <_NonCoveredAmount>_NonCoveredAmount7</_NonCoveredAmount>
            <_DeniedAmount>50.5</_DeniedAmount>
            <_ReasonCode>_ReasonCode7</_ReasonCode>
            <_PaymentAmount>50.5</_PaymentAmount>
            <_ContractAllowance>50.5</_ContractAllowance>
            <_Sequestration>50.5</_Sequestration>
            <_Deductible>50.5</_Deductible>
            <_Copay>50.5</_Copay>
            <_CoInsurance>50.5</_CoInsurance>
            <_PrimaryPayerPayment>50.5</_PrimaryPayerPayment>
        </_SERVICELINES>    
    </_PATIENTDETAILS>
</_OCROUTPUT>

我现在需要做的是在特定患者下添加额外的 _SERVICELINES。 因此,例如,如果我有新 _SERVICELINES 的另一组数据,我将如何将 go 添加到 _PatientAccNo2 下?

我假设我需要执行 Linq 查询来隔离 _PatientAccNo2,然后添加新节点及其元素。 我似乎无法弄清楚该怎么做。

这是我提出的查询(还不知道它是否正确):

XElement root = XElement.Load(filename)
IEnumerable<XElement> patientDetails =
from patient in root.Elements("_PATIENTDETAILS")
where (string)patient.Element("_PatientName") == "_PatientAccNo2"
select patient;

但在那之后我不确定如何进行

先感谢您!

尝试:

            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            XElement patient = doc.Descendants(ns + "_PATIENTDETAILS").ToList().Where(x => (string)x.Element(ns + "_PatientAccNo") == "_PatientAccNo2").FirstOrDefault();
            XElement serviceLines = patient.Element(ns + "_SERVICELINES");
            serviceLines.Add(new XElement(ns + "New_Element", "123456789"));
 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM