עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

סינון וקריאה מקובץ XML עם XPATH ב-C#

Featured Replies

פורסם

יש לי קובץ XML שנראה ככה:


<?xml version="1.0" encoding="utf-8" ?>
<gallery>
<pic filename="apple.jpg">
<title>red apple</title>
<description>this apple is red</description>
</pic>
<pic filename="banana.jpg">
<title>yellow banana</title>
<description>this banana is yello</description>
</pic>
</gallery>

לא משנה מה אני מנסה לעשות אני פשוט לא מצליח לסנן את הנתונים כדי שאני אשלח שם קובץ ואוציא את ה Title וה Description שלו.

אין לי בעיה לשנות את הקובץ XML אם המבנה שלו לא טוב.

אני אשמח אם תוכלו להראות לי דוגמא כולל איתחולי ה XPathDocument, XPathNavigator וכל אלו – למקרה שאני עושה שם משהו לא טוב...

פורסם

תרשום פה בבקשה את הקוד שכתבת כדי שנראה מה אתה לא עושה טוב.

פורסם
  • מחבר

הממממ... אני אחרי מליון נסיונות, אז אני לא כ"כ בטוח שזו הגרסא הטובה ביותר שהייתה לי (עשיתי גם כמה סינונים מסביב כדי להעיף הרבה זבל שלא קשור...), אבל הנה:


string xmlFile = Server.MapPath(Year + "/" +Month + "/" + Day +"/desc.xml");
XPathDocument xmlDoc = new XPathDocument(xmlFile);
XPathNavigator xmlNav = xmlDoc.CreateNavigator();

DirectoryInfo di = new DirectoryInfo(Server.MapPath(Year + "/" + Month + "/" + Day + "/img"));
FileInfo[] rgFiles = di.GetFiles("*.jpg");

foreach (FileInfo pic in rgFiles)
{
XPathExpression xmlExpr;
xmlExpr = xmlNav.Compile("/gallery/pic[@filename='"+ pic.Name +"']/title"); XPathNodeIterator xmlIterator = xmlNav.Select(xmlExpr);
Response.Write(xmlIterator.Current.Value);
}


[br]פורסם בתאריך: 6.11.2007 בשעה 09:51:25


טוב מצאתי פתרון לבעיה
פורסם

אנא שתף את הפתרון לבעיה שלך, כדי שאנשים אחרים יוכלו להעזר בו.

פורסם
  • מחבר

אני יותר מאשמח לשמוע הערות והצעות לשיפור.

זו דוגמא של איך להביא את שדה ה Title מקובץ ה XML



using System.Xml;
using System.Xml.XPath;
XPathDocument xmlDoc = new XPathDocument(XmlPath);
XPathNavigator xmlNav = xmlDoc.CreateNavigator();
XPathNodeIterator xmlIter;
string xmlExpr;
xmlExpr = "/gallery/pic[@filename='"apple.jpg"']/title";
xmlIter = xmlNav.Select(xmlExpr);
xmlIter.MoveNext();

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.