C#XML文档错误(2,2)反序列化xml并存储到数组

时间:2015-07-28 09:19:58

标签: c# xml xml-deserialization

目的:   - 反序列化xml文档中的数据并将其存储为数组。   - 避免手动将数据分配给不同的字符串。   - 将手动生成xml文档

public void DeserializeObject(string filename)
       {
           try
           {
               XmlSerializer deserializer = new XmlSerializer(typeof(string[]));
               FileStream fs = new FileStream(filename, FileMode.Open);
               string[] XmlData = (string[])deserializer.Deserialize(fs);

               foreach (string p in XmlData)
               {
                   Console.WriteLine(p);
               }
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
           }   
       }

XML文档如下

<?xml version="1.0" encoding="utf-8"?>
<Mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Products>
    <Product>
      <software>Seiko</software>
    </Product>
    <Product>
      <hardware>Martina</hardware>
    </Product>
  </Products>
</Mapping>

4 个答案:

答案 0 :(得分:1)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication38
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<Mapping xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
              "<Products>" +
                "<Product>" +
                  "<software>Seiko</software>" +
                "</Product>" +
                "<Product>" +
                  "<hardware>Martina</hardware>" +
                "</Product>" +
              "</Products>" +
            "</Mapping>";

            XDocument doc = XDocument.Parse(input);

            var results = doc.Descendants("Product").Select(x =>
                x.Elements().Select(y => new { type = y.Name, value = (string)y }).ToList()
            ).SelectMany(z => z).ToList();

            var groups = results.GroupBy(x => x.type).ToList();
        }
    }
}

答案 1 :(得分:1)

谢谢,找到了这个解决方案

<?xml version="1.0" encoding="utf-8" ?>
<Locations>
  <Location Name="Location1" IP="127.0.0.1"></Location>
  <Location Name="Location2" IP="127.0.0.1"></Location>
  <Location Name="Location3" IP="127.0.0.1"></Location>
  <Location Name="Location4" IP="127.0.0.1"></Location>
  <Location Name="Location5" IP="127.0.0.1"></Location>
</Locations>


using System.Xml.Linq;

class Program
   {
       static void Main(string[] args)
       {
           string[] strarr = GetStringArray("Locations.xml");

           foreach (string str in strarr)
           {
               Console.WriteLine(str);
           }
       }

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

           var locations = from l in doc.Descendants("Location")
                           select (string)l.Attribute("Name");

           return locations.ToArray();
       }
   }

答案 2 :(得分:0)

您需要从示例XML生成一个类。 您可以使用xsd.exe生成.xsd,然后从中创建.cs文件。

您需要将此类型添加到XmlSerializer

请参阅此答案:Generate C# class from XML

XmlSerializer deserializer = new XmlSerializer(typeof(Mapping)); <- Created class type here.

答案 3 :(得分:0)

如果您只想将XML文档中的数据作为字符串数组获取,则可以使用XmlDocument加载数据

XmlDocument doc = new XmlDocument();
doc.Load("file.xml");

然后,您可以使用xPath表达式找到所需的节点:

XmlNodeList nodelist = doc.SelectNodes(...);