2011-01-25 36 views
0

私はC++の初心者です。私はxmlファイルと、このコードを持っている:GetElementsByTagNameに関するCLR C++構文の質問

XmlDocument^ xml = gcnew XmlDocument(); 
xml -> Load("url.xml"); 

box -> Text = xml -> DocumentElement -> GetElementsByTagName("item") -> Item(0) -> GetElementsByTagName("title") -> Item(0) -> InnerXml; // This code doesnt work. 

とxmlファイル:

<item> 
     <pubDate>date</pubDate> 
     <title>title</title> 
     <author>author</author> 
     <description>description</description 
</item> 
<item> 
... 

私は最初の項目からタイトルタグを取得します。そして私は方法を知らない。助けてください。

upd。 私はこのコードを試してみましたが、それは動作しません:(

xml -> DocumentElement -> GetElementsByTagName("item") -> Item(0) -> ChildNodes -> GetElementsByTagName("title") -> Item(0) -> InnerXml; 

答えて

0

を私はあなたがあなたのファイルを持っているもののコピーが、私はエラーを取得すること</descriptionが終了>を持っていないという文字列を使用してxml.LoadXml()を呼び出す場合。私はそれを修正する場合は、複数のルート要素を持っているので、私はXmlExceptionを取得し、私は<item>秒を削除した場合、私はこれが私に(私のC#のコードからその場で変換された)タイトル与えることを得ることができます。

XmlElement^ item = (XmlElement)(xml->GetElementsByTagName("item")->Item(0)); 
string^ title = item->GetElementsByTagName("title")->Item(0)->InnerXml; 
0

このコードは正常に動作します。

System::String^item = xml -> DocumentElement -> GetElementsByTagName("item")->Item(0) -> OuterXml; 

XmlDocument^ xmlt = gcnew XmlDocument(); 
xmlt -> LoadXml(item); 

System::String^ title = xmlt -> DocumentElement -> GetElementsByTagName("title")->Item(0)-> OuterXml; 

box -> Text = title;