MKP
MKP

Reputation: 117

C# SelectNodes for main node attribute

I'm trying to obtain the value of the logicalName attribute of the main node of this xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ticketlayout xmlns="http://www.example.com/ticketlayout" logicalName="target.xml" deviceCode="1" measurement="mm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/ticketlayout">
    <fontdefinition id="BarCode">
         <fontname>Code128bWin</fontname>
         <size measure="pt">16</size>
    </fontdefinition>
</ticketlayout>

I've tried to add the namespace "xsi", "http://www.w3.org/2001/XMLSchema-instance" this way:

XmlDocument fLayout = new XmlDocument();
fLayout.Load("myFile.xml");
XmlNamespaceManager nsmRequestLayout = new XmlNamespaceManager(fLayout.NameTable);
nsmRequestLayout.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string sValue = fLayout.SelectNodes("//ticketlayout", nsmRequestLayout)[0].Attributes["name"].Value;

But I get no nodes. I've tried without namespace and no nodes again, and son on. ¿Could please anyone help me?

Thanks in advance.

Upvotes: 0

Views: 88

Answers (2)

Adam Chawki
Adam Chawki

Reputation: 74

First of all your XML is not valid. I modified to look this way in order to achieve what you are looking for. XML File :

<?xml version="1.0" encoding="UTF-8"?>
<ticketlayout xmlns="http://www.example.com/ticketlayout" logicalName="target.xml" deviceCode="1" measurement="mm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/ticketlayout">
    <fontdefinition id="BarCode">
         <fontname>Code128bWin</fontname>
         <size measure="pt">16</size>
    </fontdefinition>
</ticketlayout>

I am not sure why you would not have a model structure to deserialize you xml, and then access whatever property/attribute you need.

Example : Classes:

 [XmlRoot(ElementName = "size", Namespace = "http://www.example.com/ticketlayout")]
    public class Size
    {
        [XmlAttribute(AttributeName = "measure")]
        public string Measure { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "fontdefinition", Namespace = "http://www.example.com/ticketlayout")]
    public class Fontdefinition
    {
        [XmlElement(ElementName = "fontname", Namespace = "http://www.example.com/ticketlayout")]
        public string Fontname { get; set; }
        [XmlElement(ElementName = "size", Namespace = "http://www.example.com/ticketlayout")]
        public Size Size { get; set; }
        [XmlAttribute(AttributeName = "id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName = "ticketlayout", Namespace = "http://www.example.com/ticketlayout")]
    public class Ticketlayout
    {
        [XmlElement(ElementName = "fontdefinition", Namespace = "http://www.example.com/ticketlayout")]
        public Fontdefinition Fontdefinition { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }
        [XmlAttribute(AttributeName = "logicalName")]
        public string LogicalName { get; set; }
        [XmlAttribute(AttributeName = "deviceCode")]
        public string DeviceCode { get; set; }
        [XmlAttribute(AttributeName = "measurement")]
        public string Measurement { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string SchemaLocation { get; set; }
    }

Then you could use a serializer :

public class Serializer
    {
        public T Deserialize<T>(string input) where T : class
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (StringReader stringReader = new StringReader(input))
            {
                return (T)xmlSerializer.Deserialize(stringReader);
            }
        }

        public string Serialize<T>(T ObjectToSerialize)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
            StringBuilder builder = new StringBuilder();
            using (StringWriterWithEncoding textWriter = new StringWriterWithEncoding(builder, Encoding.UTF8))
            {
                xmlSerializer.Serialize(textWriter, ObjectToSerialize);
                return textWriter.ToString();
            }
        }
    }
    public class StringWriterWithEncoding : StringWriter
    {
        Encoding encoding;

        public StringWriterWithEncoding(StringBuilder builder, Encoding encoding)
        : base(builder)
        {
            this.encoding = encoding;
        }

        public override Encoding Encoding
        {
            get { return encoding; }
        }
    } 

And finally you can access whatever you want by doing the following :

var serializer = new Serializer();

//I used a local file for testing, but it should be the same thing with your file
var xmlInputData = File.ReadAllText(@"MyXmlPath");

var output = serializer.Deserialize<Ticketlayout >(xmlInputData);
var logicalName = output.LogicalName;

Upvotes: 1

Amine
Amine

Reputation: 142

If you want to get the value : target.xml Try this code

 XmlDocument fLayout = new XmlDocument();
        fLayout.Load("myFile.xml"); // your XML file
        var attrib = fLayout["ticketlayout"].Attributes["logicalName"].Value;

Upvotes: 1

Related Questions