这是一个 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;
}
但它只获得根节点值,所以请告诉解决方案。谢谢