1

I'm Trying to convert this xml to array saving the attributes names. Because I need to send this parameters to a Client using Wcf, and the client needs to display that information

<?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>

I'm using C#

Javier Luna
  • 21
  • 1
  • 1
  • 4

2 Answers2

4

There are two ways,

Using Linq to XML

You can get array of Name attributes by using linq to 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>";

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

Using XML serialization

Create class structure of your xml, deserialize xml to object and you can get list of all your required properties

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; }
    }

}

To deserialize xml use below code

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);
        }
programtreasures
  • 4,250
  • 1
  • 10
  • 29
0
string[] arr = XDocument.Load(@"FileNamewithPath.xml").Descendants("NodeName")
                    .Select(element => element.Value).ToArray();

should work for you

you can also use like this .Select(x => (int) x) if names are not their

Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51