获取XmlElement内部文本和属性

时间:2011-01-05 12:33:59

标签: asp.net xml

我有以下元素

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

我希望获得元素和属性的Innertext。

使用以下代码的属性为我提供了属性,但是如何获取元素的innerText?

我用这个

尝试了
[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; }
}

由于

2 个答案:

答案 0 :(得分:1)

您需要向您的班级添加具有XmlText属性的媒体资源:

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

现在,在反序列化之后,您应该能够从Value属性中读取内部文本。

答案 1 :(得分:0)

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

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

有关详细信息,请参阅以下内容:

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

相关问题