简体   繁体   English

C# 从 XML 子节点获取值时出现问题

[英]Having problem in C# getting value from an XML subnode

I am trying to loop through a subnode while looping through another node.我试图在循环另一个节点的同时循环一个子节点。 The code I am using is (a lot of processing code not relevant to the question removed).我正在使用的代码是(很多处理代码与删除的问题无关)。

private void InsertLineItems(IEnumerable<XElement> BillingCode, int visitId)
{
    if (BillingCode != null)
    {
        using (var visitRepo = db.GetVisitRepository())
        {
            foreach (var item in BillingCode)
            {
                if (item.Element("Code").Attribute("value").Value.Length < 9)
                {
                    foreach (var icditem in item.Descendants("ICD10List"))
                    {
                        var icd10code = item.Element("ICD10List").Element("ICD10Code").Attribute("value");
                    }
                }
            }
        }
    }
)

To parse the following XML解析以下 XML

<BillingCodes>
    <BillingCodeDTO>
        <Code value="73100"/>
        <Description value="X-ray left lower leg"/>
        <Quantity value="1"/>
        <ICD10List>
            <ICD10Code value="Y34.9"/>
            <ICD10Code value="Y34.99"/>
            <ICD10Code value="M79.9"/>
        </ICD10List>
    </BillingCodeDTO>
    <BillingCodeDTO>
        <Code value="74120"/>
        <Description value="X-ray left foot"/>
        <Quantity value="1"/>
        <ICD10List>
            <ICD10Code value="Z03.9"/>
        </ICD10List>
    </BillingCodeDTO>
</BillingCodes>

The problem I am getting is that while looping through the ICD10List it is only returning the first value.我遇到的问题是,在循环 ICD10List 时,它只返回第一个值。 Where am I going wrong in getting it to loop through all the values?让它循环遍历所有值我哪里错了? I did try using the ICD10Code as the loop (code below, just changed the foreach), that looped the correct number of times but returned the same value each time.我确实尝试使用 ICD10Code 作为循环(下面的代码,只是更改了 foreach),它循环了正确的次数,但每次都返回相同的值。

  foreach (var icditem in item.Descendants("ICD10List").Descendants("ICD10Code"))
    { var icd10code = item.Element("ICD10List").Element("ICD10Code").Attribute("value");}

a) You only have one variable which you re-assign all over again. a)您只有一个变量,您需要重新分配。 In the end you want to use a list or something.最后你想使用一个列表或其他东西。

b) you should make use of the icditem variable instead of accessing a fixed element. b)您应该使用icditem变量而不是访问固定元素。 A good IDE would indicate a warning about an unused variable.良好的 IDE 将指示有关未使用变量的警告。

foreach (var icditem in item.Descendants("ICD10List").Descendants("ICD10Code"))
{ 
    var icd10code = icditem .Attribute("value");
}

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

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