将XML转换为数组C#

时间:2017-12-31 05:27:58

标签: c# arrays xml arraylist

我试图将此xml转换为保存属性名称的数组。 因为我需要使用Wcf将此参数发送到客户端,并且客户端需要显示该信息

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <Cameras>
   <Camera Name="Camera1" Url="Camera1" Width="600" Height="800" />
   <Camera Name="Camera2" Url="Camera2" Width="600" Height="800" />
</Cameras>
</Configuration>

我正在使用C#

2 个答案:

答案 0 :(得分:2)

有两种方法,

使用Linq to XML

您可以使用linq to xml

获取Name属性数组
string testData = @"<Configuration>
                                <Cameras>
                                    <Camera Name =""Camera1"" Url = ""Camera1"" Width = ""600"" Height = ""800"" />       
                                    <Camera Name = ""Camera2"" Url = ""Camera2"" Width = ""600"" Height = ""800"" />
                                </Cameras>
                            </Configuration>";

            XDocument xdc = XDocument.Parse(testData);
            var arrNames = xdc.Root
                .Descendants("Camera")
                .Select(e => e.Attribute("Name")).ToArray();

使用XML序列化

创建xml的类结构,将xml反序列化为对象,您可以获得所有必需属性的列表

XML

<Configuration>
  <Cameras>
   <Camera Name="Camera1" Url="Camera1" Width="600" Height="800" />
   <Camera Name="Camera2" Url="Camera2" Width="600" Height="800" />
</Cameras>
</Configuration>

C#Classes

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="Camera")]
    public class Camera {
        [XmlAttribute(AttributeName="Name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName="Url")]
        public string Url { get; set; }
        [XmlAttribute(AttributeName="Width")]
        public string Width { get; set; }
        [XmlAttribute(AttributeName="Height")]
        public string Height { get; set; }
    }

    [XmlRoot(ElementName="Cameras")]
    public class Cameras {
        [XmlElement(ElementName="Camera")]
        public List<Camera> Camera { get; set; }
    }

    [XmlRoot(ElementName="Configuration")]
    public class Configuration {
        [XmlElement(ElementName="Cameras")]
        public Cameras Cameras { get; set; }
    }

}

要在代码

下反序列化xml
string testData = @"<Configuration>
                            <Cameras>
                                <Camera Name =""Camera1"" Url = ""Camera1"" Width = ""600"" Height = ""800"" />       
                                <Camera Name = ""Camera2"" Url = ""Camera2"" Width = ""600"" Height = ""800"" />
                            </Cameras>
                        </Configuration>";

        XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
        // testData is your xml string
        using (TextReader reader = new StringReader(testData))
        {
            Configuration result = (Configuration)serializer.Deserialize(reader);
        }

答案 1 :(得分:0)

P

应该适合你

如果姓名不是他们的

,你也可以像这样string[] arr = XDocument.Load(@"FileNamewithPath.xml").Descendants("NodeName") .Select(element => element.Value).ToArray(); 使用