简体   繁体   中英

Check for text value in xml

I am having an xml as

<numbers>
<number>1</number>
<number>2</number>
<number>abc</number>
</numbers>

During serialization how can i identify that the element <number> is having text as abc . I need to allow only integers not text.

假设您遇到的问题正是您要问的(并且XML处理不是问题),则可以使用int.TryParse ,它允许您输入字符串并输出结果数字(如果是)一个数字-返回值将指示解析是否成功,从而确定原始值是否实际上是一个数字。

Depending on what you understand on "to allow only intergers" the default behavior of the .net xml serialization could be enough for you. If the number member in your class is defined as int there will be an exception on deserialization your provided XML fragment:

[XmlRoot("numbers")]
public class Numbers
{
  [XmlElement("number")]
  public int[] Numbers;
}

If you want to have more control over the serialization process you can implement the IXmlSerializable interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM