XML选择节点进入列表失败

时间:2019-02-11 01:22:00

标签: c# .net xml xpath

将简单的XML文件加载到LIST中没有问题。但是,当我创建第二个元素时,它会加载,但会将所有内容加载到一行中。

我什至尝试使用xmlDoc.Descendants(“ apple”)获得相同的结果。

工程。

<?xml version="1.0" encoding="utf-8"?>
<green_apple>
    <Location>CA</Location>
    <Price>.52</Price>
</green_apple>

XDocument xmlDoc = XDocument.Load("apple.xml");
List<string> list = xmlDoc.Root.Elements()
                    .Select(element => element.Value.Trim())
                    .ToList();

List Result:
list[0] = CA
List[1] = .52

不起作用。

<?xml version="1.0" encoding="utf-8"?>
<apple>
    <green_apple>
        <Location>CA</Location>
        <Price>.52</Price>
    </green_apple>
    <red_apple>
        <Location>FL</Location>
        <Price>.71</Price>
    </red_apple>
</apple>

XDocument xmlDoc = XDocument.Load("apple.xml");
            List<string> list = xmlDoc.Root.Elements("green_apple")   <<specify specify element.
                .Select(element => element.Value.Trim())
                .ToList();

List Result:
list[0] = CA.52   <<Here's the problem, they should be in their own list element.

1 个答案:

答案 0 :(得分:0)

PKCustomKeyboard *emailCustomKeyboard = [[PKCustomKeyboard alloc] init]; PKCustomKeyboard *passwordCustomKeyboard = [[PKCustomKeyboard alloc] init]; PKCustomKeyboard *passwordConfirmCustomKeyboard = [[PKCustomKeyboard alloc] init]; PKCustomKeyboard *firstNameCustomKeyboard = [[PKCustomKeyboard alloc] init]; PKCustomKeyboard *lastNameCustomKeyboard = [[PKCustomKeyboard alloc] init]; [emailCustomKeyboard setTextView:_emailTextField]; [passwordCustomKeyboard setTextView:_passwordTextField]; [passwordConfirmCustomKeyboard setTextView:_confirmPasswordTextField]; [firstNameCustomKeyboard setTextView:_firstnameTextField]; [lastNameCustomKeyboard setTextView:_lastnameTextField]; 重新调整当前节点的子元素,在您的情况下为Elements元素。因此,您需要通过在green_apple元素上调用green_apple来获得Elements()个子节点。

使用此:

green_apple