1

我正在将 XML 文件读入这样的数据表:

DataTable dt = XMLParser.XMLRead.ReadeFormXMLFile(filePath, "my:myFields");

但是它没有捕获所有字段。

有多个从业者姓氏、从业者名字和从业者 NPI:

 <my:group1>
    <my:group2>
      <my:PractitionerFirstName>Joe</my:PractitionerFirstName>
      <my:PractitionerLastName>Limits</my:PractitionerLastName>
      <my:PractitionerNPI>0987654321</my:PractitionerNPI>
      <my:PractitionerDegree>MD</my:PractitionerDegree>
    </my:group2>
    <my:group2 xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-06T05:56:10">
       <my:PractitionerFirstName>Him</my:PractitionerFirstName>
       <my:PractitionerLastName>Haw</my:PractitionerLastName>
       <my:PractitionerNPI>1234567890</my:PractitionerNPI>
       <my:PractitionerDegree>PA</my:PractitionerDegree>
     </my:group2>
     <my:group2 xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-06T05:56:10">
        <my:PractitionerFirstName>Mike</my:PractitionerFirstName>
        <my:PractitionerLastName>Kim</my:PractitionerLastName>
        <my:PractitionerNPI>1234567890</my:PractitionerNPI>
        <my:PractitionerDegree>DO</my:PractitionerDegree>
     </my:group2>
     <my:group2 xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-06T05:56:10">
       <my:PractitionerFirstName>Vanilla </my:PractitionerFirstName>
       <my:PractitionerLastName>Ice</my:PractitionerLastName>
       <my:PractitionerNPI>1231231231</my:PractitionerNPI>
       <my:PractitionerDegree>MD</my:PractitionerDegree>
     </my:group2>
     <my:group2 xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-06T05:56:10">
       <my:PractitionerFirstName>Kim</my:PractitionerFirstName>
       <my:PractitionerLastName>John</my:PractitionerLastName>
       <my:PractitionerNPI>1234567890</my:PractitionerNPI>
       <my:PractitionerDegree>DO</my:PractitionerDegree>
     </my:group2>
  </my:group1>

但是它们没有被包括在内。它目前正在做的是将 LAST 记录纳入group2并忽略此记录之前的所有记录:

<my:PractitionerFirstName>Kim</my:PractitionerFirstName>
<my:PractitionerLastName>John</my:PractitionerLastName>
<my:PractitionerNPI>1234567890</my:PractitionerNPI>
<my:PractitionerDegree>DO</my:PractitionerDegree>

问题:什么会导致 XML 解析器只抓取最后一条记录group2而忽略其他所有内容?

4

1 回答 1

1

你可以试试这个:

XElement root = XElement.Load(filePath);
var list = root.Elements().Select(x => new
    {
        FirstName = x.Get("PractitionerFirstName", string.Empty),
        LastName = x.Get("PractitionerLastName", string.Empty),
        NPI = x.Get("PractitionerNPI", string.Empty),
        Degree = x.Get("PractitionerDegree", string.Empty)
    }).ToArray();

这适用于您提供的 xml。我正在使用从这里获取扩展方法

于 2012-04-29T00:30:56.587 回答