Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将XML中的数据添加到列表<>;?_C#_Xml_Linq_Uwp - Fatal编程技术网

C# 如何将XML中的数据添加到列表<>;?

C# 如何将XML中的数据添加到列表<>;?,c#,xml,linq,uwp,C#,Xml,Linq,Uwp,我试着从xml文件中读取数据,但它非常笨重,而且我从一个孩子那个里获取的数据很多。我在一个列表中得到了姓名、年龄等信息,因此无法将其添加到列表中 我的xml文件如下所示: <?xml version="1.0" encoding="UTF-8"?><People> <Person> <Age>30</Age> <Name>Boy</Name> <Sex>Male</

我试着从xml文件中读取数据,但它非常笨重,而且我从一个孩子那个里获取的数据很多。我在一个列表中得到了姓名、年龄等信息,因此无法将其添加到列表中

我的xml文件如下所示:

   <?xml version="1.0" encoding="UTF-8"?><People>
<Person>
    <Age>30</Age>
    <Name>Boy</Name>
    <Sex>Male</Sex>
</Person>
<Person>
    <Age>28</Age>
    <Name>Girl</Name>
    <Sex>Female</Sex>
</Person>
a.add(listTest {Name = "*DATA FROM XML*", Age ="*DATA FROM XML*", Sex="*DATA FROM XML*"});
我曾尝试使用LINQ和
p.NodeName==“xxx”
进行搜索,但似乎没有得到任何数据


有人能告诉我如何将数据从我的xml获取到列表中吗?

这里是一个简单的xml导入示例。执行此代码后,结果将反映是否找到了人员(true或false),并且
msg
将是错误消息列表(如果成功,则为空)


下面是一个简单的XML导入示例。执行此代码后,结果将反映是否找到了人员(true或false),并且
msg
将是错误消息列表(如果成功,则为空)


假设您有这个类:

public class Person
{
    public string Name { get; set; }
    public string Sex { get; set; }
    public int Age { get; set; }
}
然后,要加载XML文件,可以执行以下操作:

var doc = XDocument.Load("path to your file");

var people = doc.Root
    .Descendants("person")
    .Select(node => new Person
    {
        Name = node.Element("name").Value,
        Sex = node.Element("sex").Value,
        Age = int.Parse(node.Element("age").Value)
    })
    .ToList();

请参见

让我们假设您有这个类:

public class Person
{
    public string Name { get; set; }
    public string Sex { get; set; }
    public int Age { get; set; }
}
然后,要加载XML文件,可以执行以下操作:

var doc = XDocument.Load("path to your file");

var people = doc.Root
    .Descendants("person")
    .Select(node => new Person
    {
        Name = node.Element("name").Value,
        Sex = node.Element("sex").Value,
        Age = int.Parse(node.Element("age").Value)
    })
    .ToList();

参见

我认为XDocument在UWP中不起作用。这是我在internet上找到的所有解决方案,但似乎我必须使用:var localFolder=ApplicationData.Current.localFolder;XmlDocument XmlDocument;var file=await localFolder.TryGetItemAsync(“FoodData.xml”)作为IStorageFile;xmlDocument=wait xmlDocument.LoadFromFileAsync(文件);当您不使用XDocument时,子代将无法工作。至少对我来说不是这样。@Hudlommen编辑你的问题。注释不是为容纳大块文本而构建的,尤其是代码。
XDocument
可以在UWP应用程序中使用。您确实需要包含对
System.Xml.Linq
的引用。谢谢Tieson,T。我正要去检查一下。我认为XDocument在UWP中不起作用。这是我在internet上找到的所有解决方案,但似乎我必须使用:var localFolder=ApplicationData.Current.localFolder;XmlDocument XmlDocument;var file=await localFolder.TryGetItemAsync(“FoodData.xml”)作为IStorageFile;xmlDocument=wait xmlDocument.LoadFromFileAsync(文件);当您不使用XDocument时,子代将无法工作。至少对我来说不是这样。@Hudlommen编辑你的问题。注释不是为容纳大块文本而构建的,尤其是代码。
XDocument
可以在UWP应用程序中使用。您确实需要包含对
System.Xml.Linq
的引用。谢谢Tieson,T。我本来打算去检查一下的。工作非常顺利。VS15喜欢将其更改为XmlDocument,但这不起作用。所以,如果你遇到问题,检查一下:工作得很有魅力。VS15喜欢将其更改为XmlDocument,但这不起作用。所以如果你遇到问题,检查一下:D
var doc = XDocument.Load("path to your file");

var people = doc.Root
    .Descendants("person")
    .Select(node => new Person
    {
        Name = node.Element("name").Value,
        Sex = node.Element("sex").Value,
        Age = int.Parse(node.Element("age").Value)
    })
    .ToList();