You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Linq to XML在指定_PATIENTDETAILS节点下添加_SERVICELINES

Linq to XML:为指定_PatientAccNo的_PATIENTDETAILS节点添加_SERVICELINES节点

正在学习Linq to XML,已生成指定结构的XML文件,现需为特定_PatientAccNo(如_PatientAccNo2)对应的_PATIENTDETAILS节点添加新的_SERVICELINES节点,已写出部分Linq查询,但不清楚后续操作步骤,恳请指导。

已生成的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>

已编写的Linq查询代码

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

修正与后续步骤

1. 修正查询条件错误

原代码中where条件误将_PatientAccNo写成了_PatientName,这会导致查询不到目标节点,需要修正:

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

2. 创建新的_SERVICELINES节点

按照XML结构,用XElement构造新的服务线节点:

var newServiceLine = new XElement("_SERVICELINES",
    new XElement("_ServiceDate", "_ServiceDateNew"),
    new XElement("_CptCode", "_CptCodeNew"),
    new XElement("_Modifier", "_ModifierNew"),
    new XElement("_ServiceUnits", "_ServiceUnitsNew"),
    new XElement("_ServiceCharges", "10.0"),
    new XElement("_AllowedAmount", "10.0"),
    new XElement("_NonCoveredAmount", "_NonCoveredAmountNew"),
    new XElement("_DeniedAmount", "0.0"),
    new XElement("_ReasonCode", "_ReasonCodeNew"),
    new XElement("_PaymentAmount", "10.0"),
    new XElement("_ContractAllowance", "10.0"),
    new XElement("_Sequestration", "0.0"),
    new XElement("_Deductible", "0.0"),
    new XElement("_Copay", "0.0"),
    new XElement("_CoInsurance", "0.0"),
    new XElement("_PrimaryPayerPayment", "10.0")
);

3. 添加节点到目标_PATIENTDETAILS

因为_PatientAccNo通常是唯一标识,直接取第一个匹配的节点即可,然后将新节点添加进去:

var targetPatient = patientDetails.FirstOrDefault();
if (targetPatient != null)
{
    targetPatient.Add(newServiceLine);
}

4. 保存修改后的XML

最后将修改后的XML保存回文件:

root.Save(filename);

完整代码示例

string filename = "your-xml-file-path.xml";
XElement root = XElement.Load(filename);

// 找到目标_PATIENTDETAILS节点
var targetPatient = root.Elements("_PATIENTDETAILS")
    .FirstOrDefault(p => (string)p.Element("_PatientAccNo") == "_PatientAccNo2");

if (targetPatient != null)
{
    // 创建新的_SERVICELINES节点
    var newServiceLine = new XElement("_SERVICELINES",
        new XElement("_ServiceDate", "_ServiceDateNew"),
        new XElement("_CptCode", "_CptCodeNew"),
        new XElement("_Modifier", "_ModifierNew"),
        new XElement("_ServiceUnits", "_ServiceUnitsNew"),
        new XElement("_ServiceCharges", "10.0"),
        new XElement("_AllowedAmount", "10.0"),
        new XElement("_NonCoveredAmount", "_NonCoveredAmountNew"),
        new XElement("_DeniedAmount", "0.0"),
        new XElement("_ReasonCode", "_ReasonCodeNew"),
        new XElement("_PaymentAmount", "10.0"),
        new XElement("_ContractAllowance", "10.0"),
        new XElement("_Sequestration", "0.0"),
        new XElement("_Deductible", "0.0"),
        new XElement("_Copay", "0.0"),
        new XElement("_CoInsurance", "0.0"),
        new XElement("_PrimaryPayerPayment", "10.0")
    );

    // 添加新节点并保存修改
    targetPatient.Add(newServiceLine);
    root.Save(filename);
}

内容的提问来源于stack exchange,提问作者Rich Petersen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 13:01:05