profanis
profanis

Reputation: 2751

Get XmlElement inner Text and Attributes

I have the following element

<Bildfile typ="A" seq="1" width="320" height="214">How_Can_I_Get_This?</Bildfile>

and I want to get the Innertext of the element and the attributes.

The attributes with the following code gives me the attributes, but how could I get the innerText of the element?

I tried it with this

[XmlElement(ElementName = "Bildfile")]
public Bildfile Image { get; set; }

[Serializable]
public class Bildfile 
{
   [XmlAttribute(AttributeName = "typ")]
   public string Type { get; set; }

   [XmlAttribute(AttributeName = "seq")]
   public string Sequence { get; set; }

   [XmlAttribute(AttributeName = "width")]
   public string Width { get; set; }

   [XmlAttribute(AttributeName = "height")]
   public string Height { get; set; }
}

Thanks

Upvotes: 1

Views: 1951

Answers (2)

marc_s
marc_s

Reputation: 754478

You need to add a property with the XmlText attribute to your class:

[Serializable]
public class Bildfile 
{
   [XmlAttribute(AttributeName = "typ")]
   public string Type { get; set; }

   [XmlAttribute(AttributeName = "seq")]
   public string Sequence { get; set; }

   [XmlAttribute(AttributeName = "width")]
   public string Width { get; set; }

   [XmlAttribute(AttributeName = "height")]
   public string Height { get; set; }

   [XmlText]
   public string Value { get; set; }
}

Now, after deserializing, you should be able to read the inner text from the Value property.

Upvotes: 1

gotnull
gotnull

Reputation: 27214

XmlNode root = doc.DocumentElement;    
XmlNode objNode = root.SelectSingleNode("Bildfile");

if (objNode != null) {
     string str = objNode.Value.ToString();
}

See the following for more info:

http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx

Upvotes: 0

Related Questions