2009-09-28 7 views
0

Je dois créer un cache en utilisant un fichier XML. Voici la méthode que je vais utiliser. Je veux que cette méthode retourne l'ID basé sur le key-product_name. Donc, je veux qu'il crée une cache une fois par programme, et seulement si la clé n'est pas trouvée, alors créez une [nouvelle entrée dans le] cache. Si tout semble correct, le problème est d'obtenir l'identifiant du produit. S'il vous plaît donnez votre avis. J'ai inclus le code et le fichier XML.Besoin d'obtenir la valeur du frère

public static string getProductId(string product_name) 
    public static string getTechId(string fieldName) 
    { 
     Cache cache = HttpContext.Current.Cache; //neeed to change this. 
     string cacheNameEpm = product_name + "PrdName"; 

     if (cache[cacheNameEpm] == null) 
     { 
      XPathDocument doc = new XPathDocument(HttpContext.Current.Request.MapPath("inc/xml/prd.xml")); 
      XPathNavigator navigator = doc.CreateNavigator(); 
      string selectName = "/Products/Product/ProductName"; 
      XPathNodeIterator nodes = navigator.Select(selectName); 

      while (nodes.MoveNext()) 
      { 
       switch (nodes.Current.Name) 
       { 
        case "ProductName": 
         cacheNameEpm = nodes.Current.Value + "PrdName"; 
         navigator.Select("/Products/Product/ProductId"); 
         navigator.MoveToNext(); 
         if (nodes.Current.Name == "ProductId") 
         { 
          id = navigator.Value; 
         } 
         cache.Add(cacheNameEpm, id, null, DateTime.Now + new TimeSpan(4, 0, 0), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null); 
         break; 
       } 
      } 
     } 

     return cache[cacheNameEpm] as string; 
    } 

Voici le fichier xml:

<Products> 
    <Product> 
     <ProductName>PDPArch</ProductName> 
     <ProductId>57947</ProductId> 
    </Product> 
    <Product> 
     <ProductName>TYFTType</ProductName> 
     <ProductId>94384</ProductId> 
    </Product> 
</Products> 
+0

Il est difficile de répondre puisque vous ne dites pas ce qui ne va pas lorsque vous essayez le code ci-dessus. –

+0

Je reçois la valeur ProductName, mais je n'arrive pas à obtenir le frère qui est la valeur de l'ID du produit. C'est comme un dictionnaire a besoin d'un nom de produit et d'un identifiant. –

+0

Y a-t-il un moyen d'affecter l'objet doc au cache? –

Répondre

1

Pour obtenir l'ID de produit, disons, "TYFTType", vous auriez besoin cette expression XPath:

 
/Products/Product[ProductName = 'TYFTType']/ProductId 

Alors, que quelque chose le long les lignes de (non testé, mais vous obtenez l'idée):

public static string getProductId(string product_name) 
{ 
    Cache cache = HttpContext.Current.Cache; 

    string cacheNameEpm = product_name + "PrdName"; 
    if (cache[cacheNameEpm] == null) 
    { 
    // let's cache the navigator itself as well 
    string cacheNameNav = "prd.xml_Navigator"; 
    XPathNavigator navigator = cache[cacheNameNav] as XPathNavigator; 

    if (navigator == null) 
    { 
     XPathDocument doc = new XPathDocument(
     HttpContext.Current.Request.MapPath("inc/xml/prd.xml") 
    ); 
     navigator = doc.CreateNavigator(); 
     cache.Add(
     cacheNameNav, 
     navigator, 
     null, 
     DateTime.Now + new TimeSpan(4, 0, 0), 
     Cache.NoSlidingExpiration, 
     CacheItemPriority.Default, 
     null 
    ); 
    } 

    string xpath = String.Format(
     "/Products/Product[ProductName = '{0}']/ProductId", product_name 
    ); 

    XPathNavigator product_id = navigator.SelectSingleNode(xpath); 

    if (product_id.IsNode) 
    { 
     cache.Add(
     cacheNameEpm, 
     product_id.Value, 
     null, 
     DateTime.Now + new TimeSpan(4, 0, 0), 
     Cache.NoSlidingExpiration, 
     CacheItemPriority.Default, 
     null 
    ); 
    } 
    } 

    return cache[cacheNameEpm] as string; 
} 

Ce qui précède a deux petits problèmes:

  • Il ne se souviendra pas les noms de produits non existants.
  • Les noms de produits ne doivent pas contenir de guillemets, sinon l'expression XPath est rompue. Vous devriez ajouter une vérification pour cette condition.
Questions connexes