首页 > 解决方案 > 从 XML 标记中获取值

问题描述

我有以下代码:

p.tel.FirstOrDefault(t => t.teltype == "mobile").ToString()

<person>
  <name>Donald Duck</name>
  <tel teltype="voice" />
  <tel teltype="mobile">01000000</tel>
</person>

xml 的 c# 类如下所示:

[System.Xml.Serialization.XmlElementAttribute("tel")]
public enterprisePersonTel[] tel {
    get {
        return this.telField;
    }
    set {
        this.telField = value;
    }
}

我的代码返回:enterprisePersonTel

这不起作用

p.tel.FirstOrDefault(t => t.teltype == "mobile").Value

我怎样才能得到实际的电话号码?

标签: c#xml

解决方案


未正确生成属性的类

public partial class enterprisePersonTel

{

private string teltypeField;

private string valueField;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string teltype
{
    get
    {
        return this.teltypeField;
    }
    set
    {
        this.teltypeField = value;
    }
}

/// <remarks/>
**[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
    get
    {
        return this.valueField;
    }
    set
    {
        this.valueField = value;
    }
}**

在 Visual Studio 中使用粘贴作为类时缺少文本的 ** 部分。


推荐阅读