gpt4 book ai didi

c# - 在 Windows Phone 8 中解析 XML

转载 作者:行者123 更新时间:2023-11-30 21:56:12 24 4
gpt4 key购买 nike

我有一个如下所示的 xml 文件。

<sections>
<section>
<name>QLD Mosques</name>
<stations>
<station>
<id>101</id>
<pn>true</pn>
<name>Kuraby Mosque</name>
<url>http://sb2110.ultrastream.co.uk/kuraby</url>
<icon>...</icon>
</station>
<station>
<id>102</id>
<pn>true</pn>
<name>Gold Coast Mosque</name>
<url>http://sb2110.ultrastream.co.uk/goldcoast</url>
<icon>http://www.juju.net.au/mosquereceivers/images/icons/gc.jpg</icon>
</station>
<station>...</station>
</stations>
</section>
<section>
<name>NZ Mosques</name>
<stations>...</stations>
</section>
<section>
<name>Islamic Radio Stations</name>
<stations>...</stations>
</section>
</sections>

我想显示所有具有名为“QLD Mosques”的“部分”的站名。例如,我的结果将是“Kuraby Mosque,Gold Coast Mosque,...”。我怎样才能达到结果??

乙:乙:我可以使用以下代码在“部分”标签(给出结果 QLD Mosques、NZ Mosques、Islamic Radio Stations)下显示名称:

public static List<MyData> channel_main_list = new List<MyData>();

public MainChannelList()
{
InitializeComponent();


WebClient client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri("http://www.juju.net.au/mosquereceivers/Stations.xml",UriKind.Absolute));

}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
return;

Stream str = e.Result;

string node = "section";

XDocument loadedData = XDocument.Load(str);

try
{
foreach (var item in loadedData.Descendants(node))
{

try
{
MyData m = new MyData();
m.channel_name = item.Element("name").Value;

channel_main_list.Add(m);
}
catch (Exception)
{

MessageBox.Show("Problem");
}



}

listBox.ItemsSource = channel_main_list;

}
catch (Exception)
{
MessageBox.Show("Connectivity Problem");
}
}

最佳答案

这是一种可能的方式,假设 XML 结构是一致的,并且总是能找到正在搜索的 name :

var result = loadedData.Descendants("section")
.Where(o => (string)o.Element("name") == "QLD Mosques")
.Elements("stations")
.Elements("station")
.Elements("name");

Dotnetfiddle Demo

关于c# - 在 Windows Phone 8 中解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673864/

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