0

linqtoxmlによってこのxmlファイルを選択ipして要素を形成する方法port

<?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 に答える