1

Linq から XML クエリにデータを返すのに問題があります。私は次のXMLを持っています

<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
  <publshInformation>
    <Publish_Date>03/14/2013</Publish_Date>
    <Record_Count>5440</Record_Count>
  </publshInformation>
</sdnList>

次を使用して公開日の値を取得しようとしています

XDocument xDoc = XDocument.Load(fileName);
XNamespace xNS = "http://tempuri.org/sdnList.xsd";
XNamespace xNS1 = "http://www.w3.org/2001/XMLSchema-instance";
string strCurrentDate = xDoc.Element(xNS1 + sdnList").Element("publshInformation").Element("Publish_Date").Value; 

これは、オブジェクトの予期されるエラーを返すだけです。私の問題は名前空間にあることを知っています(そして、おそらく単純な解決策になるでしょう)

ありがとう

4

2 に答える 2

2

そのドキュメントのすべての要素はhttp://tempuri.org/sdnList.xsd名前空間にあるため、次のようなものが必要です

xDoc.Element(xNS + "sdnList")
    .Element(xNS + "publshInformation")
    .Element(xNS + "Publish_Date").Value;
于 2013-03-18T15:13:49.490 に答える
1

これは機能します:

XNamespace xNS = "http://tempuri.org/sdnList.xsd";
string strCurrentDate = xDoc.Element(xNS + "publshInformation").Element(xNS + "Publish_Date").Value;
于 2013-03-18T15:28:57.217 に答える