2

我正在使用 Linq to XML 将列表保存到 XML 字符串中。

我试图获取的 xml 字符串:

<people>
<name>xxx</name>
<age>23</age>
</people>
<people>
<name>yyy</name>
<age>25</age>
</people>

C#代码:

List<Peoples> peopleList = new List<Peoples>(); 
peopleList.Add(new Peoples() { Name = "xxx", Age = 23 });
peopleList.Add(new Peoples() { Name = "yyy", Age = 25 });
var people (from item in peopleList
select new XElement("people",
                                new XAttribute("name", item.Name),
                                new XAttribute("age", item.Age)
                            ));

我怎样才能转换var peopleXML string

谢谢你。


编辑:

我能想到的是在xml中添加一个根元素,并用空字符串替换和。


Jignesh Thakker 解决方案有效:

string str = people.Select(x => x.ToString()).Aggregate(String.Concat); 
4

4 回答 4

3

您可以通过两种解决方案获取 XML 字符串。

解决方案 1:要获取 XML 字符串,您需要将 XmlElement 放在 XDocument 对象中。尝试,

  List<Peoples> peopleList = new List<Peoples>(); 
  peopleList.Add(new Peoples() { Name = "xxx", Age = 23 });
  peopleList.Add(new Peoples() { Name = "yyy", Age = 25 });
  var people =  (from item in peopleList
  select new XElement("people",
                            new XElement("name", item.Name),
                            new XElement("age", item.Age)
                        ));

  XElement root = new XElement("Peoples");
  root.Add(people);
  XDocument xDoc = new XDocument(
                         new XDeclaration("1.0", "utf-8", "yes"),
                         root);
  string str = xDoc.ToString();

您需要根元素来获取 Xml 字符串。

输出:

<Peoples>
  <people>
    <name>xxx</name>
    <age>23</age>
  </people>
  <people>
    <name>yyy</name>
    <age>25</age>
  </people>
</Peoples> 

这里将姓名和年龄视为 XElement。正如您提到的有问题的代码XAttribute。如果您想将姓名和年龄视为XAttribute.

   List<Peoples> peopleList = new List<Peoples>(); 
   peopleList.Add(new Peoples() { Name = "xxx", Age = 23 });
   peopleList.Add(new Peoples() { Name = "yyy", Age = 25 });
   var people =  (from item in peopleList
                           select new XElement("people",
                            new XAttribute("name", item.Name),
                            new XAttribute("age", item.Age)
                        ));

    XElement root = new XElement("Peoples");
    root.Add(people);
    XDocument xDoc = new XDocument(
                         new XDeclaration("1.0", "utf-8", "yes"),
                         root);


    string str = xDoc.ToString(); 

输出:

<Peoples>
  <people name="xxx" age="23" />
  <people name="yyy" age="25" />
</Peoples>

解决方案 2:如果您想要来自以下的 Xml 字符串,请尝试以下操作List<XElement>

string str = people.Select(x => x.ToString()).Aggregate(String.Concat);

如果 XElement 用于姓名和年龄,则输出:

  <people>
    <name>xxx</name>
    <age>23</age>
  </people>
  <people>
    <name>yyy</name>
    <age>25</age>
  </people>

如果 XAttribute 用于姓名和年龄,则输出:

  <people name="xxx" age="23" />
  <people name="yyy" age="25" />

希望它应该工作。解决方案 2 最适合您的需要。

于 2012-09-19T05:47:30.723 回答
2

您希望“姓名”和“年龄”成为元素,而不是属性。由于您所需的输出中没有顶部元素。它是两个元素的顺序输出。

void Main()
{
    List<Peoples> peopleList = new List<Peoples>(); 
    peopleList.Add(new Peoples() { Name = "xxx", Age = 23 });
    peopleList.Add(new Peoples() { Name = "yyy", Age = 25 });
    var people =(from item in peopleList
    select new XElement("people",
                                    new XElement("name", item.Name),
                                    new XElement("age", item.Age)
                                ));
    Console.WriteLine (people.First());
    Console.WriteLine (people.Last());
}

class Peoples
{
    public string Name {get;set;}
    public int Age {get;set;}
}

编辑#1:我想强调的是,您想要的输出不是单个 xml 输出。如果我添加一个根对象,你会非常接近。试试这个:

XElement root = new XElement("root");
foreach (var item in peopleList)
{
    root.Add(new XElement("people",
                                new XElement("name", item.Name),
                                new XElement("age", item.Age)
                            ));         
}
Console.WriteLine (root.ToString());
于 2012-09-19T03:50:52.807 回答
0

也许这会给你你所需要的

List<Peoples> peopleList = new List<Peoples>();
            peopleList.Add(new Peoples() { Name = "xxx", Age = 23 });
            peopleList.Add(new Peoples() { Name = "yyy", Age = 25 });

            var people = new XElement("Peoples", peopleList.Select(p => 
                                                             new XElement("people",
                                                                          new XAttribute("name", p.Name),
                                                                          new XAttribute("age", p.Age)
                                                                 ))).ToString();

这将产生以下 XML

<Peoples>
  <people name="xxx" age="23" />
  <people name="yyy" age="25" />
</Peoples>
于 2012-09-19T03:55:53.373 回答
0
class Program
{
    static void Main(string[] args)
    {
        List<Person> peopleList = new List<Person>(); 
        peopleList.Add(new Person() { Name = "xxx", Age = 23 });
        peopleList.Add(new Person() { Name = "yyy", Age = 25 });

        XElement xmlDoc = new XElement("people", from p in peopleList
                                                 select new XElement("person",
                                                                     new XElement("name", p.Name),
                                                                     new XElement("age", p.Age)));

        Console.WriteLine(xmlDoc.ToString());
        Console.ReadKey();
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

将产生:

<people>
  <person>
    <name>xxx</name>
    <age>23</age>
  </person>
  <person>
    <name>yyy</name>
    <age>25</age>
  </person>
</people>
于 2012-09-19T04:09:21.290 回答