gpt4 book ai didi

c# - 我们如何从 xml 文件中获取所有子值

转载 作者:行者123 更新时间:2023-11-30 22:23:45 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于c# - 我们如何从 xml 文件中获取所有子值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13250083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com