2012-04-27 78 views
1

我讀一個XML文件轉換成一個數據表是這樣的:XMLParser中跳過最後標籤在標籤集合

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

但沒有捕捉到的所有字段。

有多種practitionerlastnames和practitionerfirstnames和practitionerNPIs:但是沒有被列入他們

<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> 

。它目前正在做什麼,走的是最後一個記錄group2,而忽略所有記錄前述這一項:

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

問題:什麼會導致XML解析器從group2搶到只有最後一條記錄,而忽略其他一切?

+0

請解釋downvote,我很樂意改變/添加信息 – 2012-04-27 22:40:36

+0

我不知道你爲什麼得到了downvotes。問題:這些字段是否忽略了具有自定義名稱空間的字段?這可能與爲什麼(我沒有xmlparse經驗btw) – payo 2012-04-27 22:59:45

+0

@payo非常感謝您的迴應。我是新來的XML,所以我不知道這是什麼意思:) – 2012-04-27 23:04:05

回答

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。我正在使用Get extension method from here