1

我想解析这个 XML 并获取手机号码

<Result>
  <text_message>
    <id>4394</id>
    <message>2</message>
    <time>1373998420</time>
    <message_phone>
      <cellphone>09102805755</cellphone>
    </message_phone>
  </text_message>
</Result>

我的代码是:

int Num;
XDocument doc = XDocument.Parse(sms.InboxCheck(authsms, "1", "1", "0"));
doc.Save(Server.MapPath(".") + "\\xmlfolder\\xmlfile2.xml");
ds.ReadXml(Server.MapPath(".") + "\\xmlfolder\\xmlfile2.xml");
Num = Convert.ToInt32(ds.Tables["text_message"].Rows[0][3].ToString());
Label1.Text= Num.ToString();

这个值Rows[0][3]0

如何获取手机号码?

4

4 回答 4

5

继续利用XDocument班级怎么样?

XDocument doc = XDocument.Load(sms.InboxCheck(authsms, "1", "1", "0"));
var cellphone = doc.Root.Element.Elements("message_phone").First().Element.Value
于 2013-07-17T14:26:50.833 回答
3

做这样的事情

int num = int.parse(doc.Descendants.("cellphone").Single().Value);

或者更好

string num = doc.Descendants.("cellphone").Single().Value;
于 2013-07-17T14:26:35.287 回答
0

您是否考虑过使用序列化?

您可以将 xml 映射到数据结构(类)并通过XmlSerializeror读取或/和写入它DataContractSerializer

于 2013-07-17T14:33:09.023 回答
0

我会XmlDocument改用。

XmlDocument doc = new XmlDocument();
doc.Load("myFile.xml");
XmlNode phone = doc.SelectSingleNode("/Result/text_message/message_phone/cellphone");
string number = phone.InnextText; 
于 2013-07-17T15:19:23.163 回答