2012-01-27 5 views
0

次のような問題があります。私は初心者です。これはおそらく理由です。XAMLのリソースであるときにC#からXMLノードを読み取る

私はいくつかの画像を表示するリストボックスを持っており、XMLファイルからこれらの画像のパスを取得します。このXMLファイルは、XAMLのリソースとして定義されています。画像が選択され、ユーザーがenterを押すと、そのXMLファイルの別のノードにあるパス(下の例ではappath)を含むいくつかのパラメータを持つ外部アプリを起動します。

XMLレイアウト:

<picture> 
    <path></path> 
    <appath></appath> 
</picture> 

私はC#からノードにアクセスする方法を見つけるように見えることはできません。 すべてのポインタが大いに感謝!事前に

おかげで、 J.

+0

は、いくつかのコンテキストコードを使用することができます。 –

答えて

0

あなたは画像ノード(ある種のID)あなたのいずれかの属性を持っていない場合は、まずあなたが既に持っている必要がありますパスに一致しなければなりませんあなたのリストボックス、appathを返します。あなたのxmlファイルを想定し

static string GetAppath(string xmlString, string picPath) 
    { 
     string appath = String.Empty; 
     XmlDocument xDoc = new XmlDocument(); 
     xDoc.LoadXml(xmlString); 

     XmlNodeList xList = xDoc.SelectNodes("/picture"); 
     foreach (XmlNode xNode in xList) 
     { 
      if (xNode["path"].InnerText == picPath) 
      { 
       appath = xNode["appath"].InnerText; 
       break; 
      } 
     } 
     return appath; 
    } 
0

に見え、何かのように:

XElement resource = XElement.Parse(Properties.Resources.Pictures); 

これらの拡張機能を使用する:(ちょうどあなたのルートにクラス/ファイルをコピーし、あなたのリソース名が写真であれば

<?xml version="1.0" encoding="ISO-8859-1"?> 
<pictures> 
<picture> 
    <path></path> 
    <appath></appath> 
</picture> 
</pictures> 

あなたのプロジェクトのディレクトリ)http://searisen.com/xmllib/extensions.wiki

public class PicturesResource 
{ 
    XElement self; 
    public PicturesResource() 
    { self = XElement.Parse(Properties.Resources.Pictures); } 

    public IEnumerable<Picture> Pictures 
    { get { return self.GetEnumerable("picture", x => new Picture(x)); } } 
} 

public class Picture 
{ 
    XElement self; 
    public Pictures(XElement self) { this.self = self; } 

    public string Path { get { return self.Get("path", string.Empty); } } 
    public string AppPath { get { return self.Get("apppath", string.Empty); } } 

} 

その後、写真を結合することができるか、それらを見上げるん:

PicturesResource pictures = new PicturesResource(); 
foreach(Picture pic in pictures.Pictures) 
{ 
    string path = pic.Path; 
    string apppath = pic.AppPath; 
} 

または特定の画像を検索:

Picture pic = pictures.FirstOrDefault(p => p.Path = "some path"); 
if(pic != null) 
{ 
    // do something with pic 
} 
関連する問題