2

这是一个 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

4 回答 4

2

使用LINQ2XML..它的方式太简单太酷了

它完全替代了其他xmlapi

XElement doc=XElement.Load("yourXML.xml");
doc.Descendants("AppBuilderProject").Element("Name").Value;//name value
doc.Descendants("AppBuilderForms").Descendants("AppBuilderForm").Element("Title").Value;//title value
于 2012-11-06T11:38:14.897 回答
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,将包含在文档中任何位置命名的任何元素。

于 2012-11-06T11:38:32.283 回答
0

使用XDocument/XElement它会更容易,你也可以很容易地做 linq ..

XElement xmlObject = XElement.Load(fileName);
var xDoc = xmlObject.Descendants("AppBuilderProject") //will return you all the elements
于 2012-11-06T11:36:37.483 回答
0

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

using (var fs = new FileStream("somedata.xml", FileMode.Open))
{
    var Values = XElement.Load(fs).DescendantNodes()
                         .Where(item => item.NodeType == XmlNodeType.Text);
}
于 2012-11-06T11:37:25.710 回答