我们如何从xml文件中获取所有子元素值

时间:2012-11-06 11:29:20

标签: c# xml

这是一个XML文件:

<AppBuilderProject>
    <Name>Project1</Name>
    <AppBuilderForms>
        <AppBuilderForm>
            <Title>Form1</Title>
            <AppBuilderFormObjects>
                <CheckMarkObject>
                    <Name>subscribeCheckmark</Name>
                    <Label>Subscribe</Label>
                    <IsChecked>1</IsChecked>
                </CheckMarkObject>
                <DateTimeObject>
                    <Name>dateOfBirth</Name>
                    <Label>Date of Birth</Label>
                </DateTimeObject>
                <LocationObject>
                    <Name>mapLocation</Name>
                    <Label>Any Location</Label>
                    <Longitude>1.23453</Longitude>
                    <Latitude>1.2345</Latitude>
                </LocationObject>
                <SwitchObject>
                    <Name>newsletterSwitch</Name>
                    <Label>Receive Newsletter</Label>
                    <IsChecked>1</IsChecked>
                </SwitchObject>
                <TextViewObject>
                    <Name>detailTextView</Name>
                    <Value>Details</Value>
                </TextViewObject>
                <TextFieldObject>
                    <Name>nameTextField</Name>
                    <Label>Name</Label>
                    <Value>name</Value>
                </TextFieldObject>
                <PhotoPickerObject>
                    <Name>photoPicker</Name>
                    <Label>Pick Photo</Label>
                </PhotoPickerObject>
                <SpinWheelPickerObject>
                    <Name>comboBox</Name>
                    <Label>Countries</Label>
                    <SpinWheelPickerColumns>
                        <SpinWheelPickerColumnObject>
                            <Name>name</Name>
                            <Label>Name</Label>
                            <SpinWheelPickerItems>
                                <SpinWheelPickerItemObject>
                                    <Name>Pakistan</Name>
                                    <Label>Pakistan</Label>
                                </SpinWheelPickerItemObject>
                                <SpinWheelPickerItemObject>
                                    <Name>United Arab Emirates</Name>
                                    <Label>United Arab Emirates</Label>
                                </SpinWheelPickerItemObject>
                            </SpinWheelPickerItems>
                        </SpinWheelPickerColumnObject>
                        <SpinWheelPickerColumnObject>
                            <Name>code</Name>
                            <Label>Code</Label>
                            <SpinWheelPickerItems>
                                <SpinWheelPickerItemObject>
                                    <Name>+92</Name>
                                    <Label>+92</Label>
                                </SpinWheelPickerItemObject>
                                <SpinWheelPickerItemObject>
                                    <Name>+971</Name>
                                    <Label>+971</Label>
                                </SpinWheelPickerItemObject>
                            </SpinWheelPickerItems>
                        </SpinWheelPickerColumnObject>
                    </SpinWheelPickerColumns>
                </SpinWheelPickerObject>
            </AppBuilderFormObjects>
        </AppBuilderForm>
    </AppBuilderForms>
</AppBuilderProject>

我希望得到所有的价值观。 我使用此代码查找XML中的所有值:

此代码仅读取根节点:

XmlDocument DOC = new XmlDocument();
//  DOC.RemoveAll();
DOC.Load("C:\\Users\\DIGITEL EYE SYSTEM\\Desktop\\response.xml");
XmlNode firstnode = DOC.SelectSingleNode("AppBuilderProject");
Project.Name = firstnode["Name"].InnerText;
XmlNode secondnode = DOC.SelectSingleNode("AppBuilderProject/AppBuilderForm");
XmlNodeList controlingnode = secondnode.SelectNodes("Title");



XmlNodeList ParentNode = DOC.GetElementsByTagName("AppBuilderProject");

foreach (XmlNode AllNodes in ParentNode)
{
    Project.Name = AllNodes["Name"].InnerText;


    if (AllNodes.ChildNodes == DOC.GetElementsByTagName("AppBuilderForms"))
    {
        //  Project.Forms = DOC.GetElementsByTagName("");
        // String sb = AllNodes["Forms"].InnerText;
    }
    XmlNodeList checkmarknode = DOC.GetElementsByTagName("CheckMarkObject");

    if (checkmarknode.Item(0).InnerText == "Name")
    {
        checkmark.Name = checkmarknode[0].InnerText;
    }
    else if (checkmarknode.Item(1).InnerText == "Label")
    {
        checkmark.Label = checkmarknode[1].InnerText;
    }
    // if (AllNodes.ChildNodes == DOC.GetElementsByTagName("CheckMarkObject"))
    //  {
    //     checkmark.Name = AllNodes["Name"].InnerText;


    //   checkmark.Label = AllNodes["Label"].InnerText;
    // checkmark.IsChecked = AllNodes["IsChecked"].InnerText;
    // }

    if (ParentNode == DOC.GetElementsByTagName("DateTimeObject"))
    {
        DateTime.Name = AllNodes["Name"].InnerText;
        DateTime.Label = AllNodes["Label"].InnerText;
    }

    if (ParentNode == DOC.GetElementsByTagName("LocationObject"))
    {
        Location.Name = AllNodes["Name"].InnerText;
        Location.Label = AllNodes["Label"].InnerText;
        Location.Longitude = AllNodes["Longitude"].InnerText;
        Location.Latitude = AllNodes["Latitude"].InnerText;
    }

    if (ParentNode == DOC.GetElementsByTagName("SwitchObject"))
    {
        Switch.Name = AllNodes["Name"].InnerText;
        Switch.Label = AllNodes["Label"].InnerText;
        // Switch.IsChecked = AllNodes["IsChecked"].InnerText;
    }

    if (ParentNode == DOC.GetElementsByTagName("TextViewObject"))
    {
        TextView.Name = AllNodes["Name"].InnerText;
        TextView.Value = AllNodes["Value"].InnerText;
    }

    if (ParentNode == DOC.GetElementsByTagName("TextFieldObject"))
    {
        TextField.Name = AllNodes["Name"].InnerText;
        TextField.Value = AllNodes["Value"].InnerText;
    }

    if (ParentNode == DOC.GetElementsByTagName("PhotoPickerObject"))
    {
        PhotoPicker.Name = AllNodes["Name"].InnerText;
        PhotoPicker.Label = AllNodes["Label"].InnerText;
    }

    if (ParentNode == DOC.GetElementsByTagName("SpinWheelPickerObject"))
    {
        SpinWheelPicker.Name = AllNodes["Name"].InnerText;
        SpinWheelPicker.Label = AllNodes["Label"].InnerText;
        // SpinWheelPicker.Columns = AllNodes["Columns"].InnerText;
    }

但它只获得根节点值,所以请告诉解决方案。谢谢

4 个答案:

答案 0 :(得分:2)

使用LINQ2XML ..过于简单和酷炫

它完全取代了其他xml api的

XElement doc=XElement.Load("yourXML.xml");
doc.Descendants("AppBuilderProject").Element("Name").Value;//name value
doc.Descendants("AppBuilderForms").Descendants("AppBuilderForm").Element("Title").Value;//title value

答案 1 :(得分:1)

一般情况下,您不希望通过将所有元素都视为同一级别的层次结构来浏览XmlDocument - Xml具有层次结构,可以包含复杂的关系,如1:N,嵌套等。

所以不要像

那样循环和编写代码
 if (ParentNode == DOC.GetElementsByTagName("SpinWheelPickerObject"))
 {
   SpinWheelPicker.Name = AllNodes["Name"].InnerText;
   ...
 }

使用XmlDocument / XmlNode,最好使用Xpath导航到您需要的元素/属性,例如。

   SpinWheelPicker.Name = 
     DOC.SelectSingleNode("/AppBuilderProject//SpinWheelPickerObject/Name")
        .InnerText;

修改

要迭代1:N关系,您应该使用foreach来迭代集合,例如

foreach (XmlNode node in DOC.SelectNodes("//SpinWheelPickerItemObject"))
{
   System.Diagnostics.Trace.WriteLine(node.InnerText);
}

此处我们还使用//快捷方式来避免遍历层次结构。但请注意,这会忽略层次结构 - 即文档中任何位置的任何名为SpinWheelPickerItemObject的元素都将被包含在内。

答案 2 :(得分:0)

使用XDocument / XElement会更容易,而且你也可以轻松地做linq ..

XElement xmlObject = XElement.Load(fileName);
var xDoc = xmlObject.Descendants("AppBuilderProject") //will return you all the elements

答案 3 :(得分:0)

这个LINQ可以完成这项工作。想法是获取所有的XmlNodes并检查它是否为Text类型。

using (var fs = new FileStream("somedata.xml", FileMode.Open))
{
    var Values = XElement.Load(fs).DescendantNodes()
                         .Where(item => item.NodeType == XmlNodeType.Text);
}