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#

0

2 Answers 2

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);
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Actually a better way to implement but OP asked for Array so provided the solution like that
@ArijitMukherjee OP will make decision how he will implement and whats fit with his solution
How can I flatten that so I get <code> Configuration{ Camera [] cameras; } </code> So I don't need an surrounding "Cameras" class?
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

1 Comment

In your case it will be Camera

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.