0

私は奇妙な問題を抱えており、XDocumentに関するすべてを試しました。

「CategoryClass」ノードの値を取得し、「ListBoxClass」タイプの独自のオブジェクトにサブノードを設定したいと思います。しかし、LINQクエリは何も返しません。

System.Xml.Linq.XNamespace lName = xDoc.Root.GetDefaultNamespace();
        System.Xml.Linq.XNamespace lANamespace = "http://schemas.datacontract.org/2004/07/KRefreshService";

        var lEle = from xml2 in xDoc.Element(lName + "GetCategoriesResponse").Element(lName + "GetCategoriesResult").Elements(lANamespace + "CategoryClass")
                                                    select new ListBoxClass 
                                                    {
                                                        Id =  (int)xml2.Element(lName + "ID"),
                                                        Name = xml2.Element(lName+ "CatName").ToString(),
                                                        Description = xml2.Element(lName + "CatDescription").ToString()
                                                    };

これがXMLです

<GetCategoriesResponse xmlns="http://tempuri.org/">
 <GetCategoriesResult xmlns:a="http://schemas.datacontract.org/2004/07/KRefreshService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:CategoryClass>
  <a:CatDescription>General questions regarding IQ. These questions will help you prepare for the interviews.</a:CatDescription>
  <a:CatName>IQ</a:CatName>
  <a:ID>1</a:ID>
</a:CategoryClass>
<a:CategoryClass>
  <a:CatDescription>This category will help you improve your general knowledge. It have information from all the different subjects.</a:CatDescription>
  <a:CatName>General Knowledge</a:CatName>
  <a:ID>2</a:ID>
</a:CategoryClass>
<a:CategoryClass>
  <a:CatDescription>If you feel that you computer science basics are slipping by your head as you involve more in technology. No problem! subscribe to this category.</a:CatDescription>
  <a:CatName>Computer Science</a:CatName>
  <a:ID>3</a:ID>
</a:CategoryClass>

4

1 に答える 1

1

問題は、クエリのこの部分です。

select new ListBoxClass 
{
    Id =  (int)xml2.Element(lName + "ID"),
    Name = xml2.Element(lName+ "CatName").ToString(),
    Description = xml2.Element(lName + "CatDescription").ToString()
};

ここで間違った名前空間を使用しています - これらの要素にはa接頭辞があります:

<a:CatDescription>...</a:CatDescription>
<a:CatName>IQ</a:CatName>
 <a:ID>1</a:ID>

...だから彼らは namespace を使用していますhttp://schemas.datacontract.org/2004/07/KRefreshService"。代わりに、ドキュメントのルート要素で宣言された名前空間を使用しています。

また、明示的な変換によって取得する傾向があるeachのが必要になると思います。を呼び出すと、要素のインデントされた XML が得られます。あなたが望んでいるのは:XElementstringToString()

select new ListBoxClass 
{
    Id =  (int) xml2.Element(lANamespace + "ID"),
    Name = (string) xml2.Element(lANamespace + "CatName"),
    Description = (string) xml2.Element(lANamespace + "CatDescription")
};

(ちなみに、なぜ完全に修飾System.Xml.Linq.XNamespaceされているのか、または変数のプレフィックスを持っているのかは明らかlではありません...このコードを機能させたときに、このコードをより明確にするために多くのことができます。)

編集:あなたが言ったコードは動作しません:

using System;
using System.Linq;
using System.Xml.Linq;

public class Test
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load("test.xml");
        XNamespace envelopeNs = doc.Root.GetDefaultNamespace();
        XNamespace resultNs = "http://schemas.datacontract.org/2004/07/KRefreshService";

        Console.WriteLine(doc.Element(envelopeNs + "GetCategoriesResponse")
                             .Element(envelopeNs + "GetCategoriesResult")
                             .Elements(resultNs + "CategoryClass")
                              .Count());
    }
}

それは 3 を出力します。それで問題が発生した場合は、私たちに伝えていないことがあるはずです。

于 2012-10-24T15:52:16.703 に答える