将XML文档中的值取为字符串数组

时间:2023-01-14 20:12:04

I'm attempting to take values from a XML file and put them into a string array. Here's the code I'm using to accomplish this:

我正在尝试从XML文件中获取值并将它们放入字符串数组中。这是我用来完成这个的代码:

public static string[] GetStringArray(string path)
{
    var doc = XDocument.Load(path);

    var services = from service in doc.Descendants("Service")
                    select (string)service.Attribute("name");

    return services.ToArray();
}

But whenever I use it I get a NullReferenceException here:

但每当我使用它时,我在这里得到一个NullReferenceException:

foreach (string @string in query)
    WeatherServicesCBO.Items.Add(@string);

Of this method:

这个方法:

public void InitializeDropDown(string XmlFile, string xpath)
{

    //string[] services = { "Google Weather", "Yahoo! Weather", "NOAA", "WeatherBug" };
    string[] services = GetStringArray("SupportedWeatherServices.xml");
    IEnumerable<string> query = from service in services
                                orderby service.Substring(0, 1) ascending
                                select service;

    foreach (string @string in query)
        WeatherServicesCBO.Items.Add(@string);
}

EDIT Here's the XML file being used

编辑这是正在使用的XML文件

<?xml version="1.0" encoding="utf-8" ?>
<SupportedServices>
  <Service>
    <name>Google Weather</name>
    <active>Yes</active>
  </Service>
  <Service>
    <name>WeatherBug</name>
    <active>No</active>
  </Service>
  <Service>
    <name>Yahoo Weather</name>
    <active>No</active>
  </Service>
  <Service>
    <name>NOAA</name>
    <active>No</active>
  </Service>
</SupportedServices>

4 个解决方案

#1


4  

The XML has a name element. You are attempting to read the name attribute. There is none so you get null back. Make the appropriate changes.

XML有一个名称元素。您正在尝试读取name属性。没有,所以你得到null。进行适当的更改。

var services = from service in doc.Descendants("Service")
                select (string)service.Element("name");

#2


3  

select (string)service.Attribute("name");

"name" is not an attribute of service. it is a child element.

“名称”不是服务的属性。它是一个儿童元素。

#3


2  

name is not an attribute of Service but a child element. You should modify your GetStringArray query to:

name不是Service的属性,而是子元素。您应该将GetStringArray查询修改为:

var services = from service in doc.Descendants("Service")
               select service.Element("name").Value;

#4


1  

Get node list in Array:

在Array中获取节点列表:

XmlDocument xDocument;
xDocument.Load(Path);
var xArray = xDocument.SelectNodes("SupportedServices/Service/name");

#1


4  

The XML has a name element. You are attempting to read the name attribute. There is none so you get null back. Make the appropriate changes.

XML有一个名称元素。您正在尝试读取name属性。没有,所以你得到null。进行适当的更改。

var services = from service in doc.Descendants("Service")
                select (string)service.Element("name");

#2


3  

select (string)service.Attribute("name");

"name" is not an attribute of service. it is a child element.

“名称”不是服务的属性。它是一个儿童元素。

#3


2  

name is not an attribute of Service but a child element. You should modify your GetStringArray query to:

name不是Service的属性,而是子元素。您应该将GetStringArray查询修改为:

var services = from service in doc.Descendants("Service")
               select service.Element("name").Value;

#4


1  

Get node list in Array:

在Array中获取节点列表:

XmlDocument xDocument;
xDocument.Load(Path);
var xArray = xDocument.SelectNodes("SupportedServices/Service/name");