0

我如何通过 linq to xml 选择ipport元素形成这个 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <IncomingConfig>
    <ip>10.100.101.18</ip>
    <port>5060</port>
  </IncomingConfig>
  <Device>
    <username>tarek</username>
    <AgentName>tarek</AgentName>
    <password>ffff</password>

  </Device>
  <Device>
    <username>adf</username>
    <AgentName>adf</AgentName>
    <password>fadsf</password>

  </Device>
</settings>

我写了这段代码但不工作

            XDocument doc = XDocument.Load(CONFIGURATION_FULL_PATH);
            var port = int.Parse(doc.XPathSelectElement("port").Value);
            var localIpAdres = doc.XPathSelectElement("ip").Value;
4

2 回答 2

1

如果您已将文件加载到doc变量中,您只需要

string localIpAddress = doc.Root.Element("IncomingConfig").Element("ip").Value;
string port = doc.Root.Element("IncomingConfig").Element("port").Value;
于 2012-08-26T15:51:14.677 回答
0

尝试

doc.XPathSelectElement("//port").Value

参数必须是 XPath 表达式。

于 2012-08-26T15:52:09.317 回答