這是我的。我使用在線存儲的XML文件,將它們下載到Stream中,然後將Stream傳遞給XMLReader類。所有這些都是CrossPlatform代碼。
如果您想將@xml文件作爲應用程序資源嵌入,如@Dimitris Batsougiannis在其評論中解釋的那樣,這是第二種選擇。但是,一旦你有你的流,代碼將是相同的。簡單地說,將您的流傳遞給BuildItemList方法。
public class ItemHelper
{
public static bool IsReadingXML { get; set; }
public static List<Item> ItemList { get; set; }
public static void BeginReadXMLStream(string currFileName)
{
IsReadingXML = true;
string ImagesRootFolder = "http://www.mywebsite.com/";
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ImagesRootFolder + currFileName);
httpRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), httpRequest);
}
private static void FinishWebRequest(IAsyncResult result)
{
IsReadingXML = true;
HttpWebResponse httpResponse = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
Stream httpResponseStream = httpResponse.GetResponseStream();
BuildItemList(httpResponseStream);
}
}
public static void BuildItemList(Stream xmlStream)
{
string ImagesRootFolder = "http://www.mywebsite.com/";
List<Item> returnValue = new List<Item>();
try
{
using (XmlReader myXMLReader = XmlReader.Create((xmlStream)))
{
while (myXMLReader.Read())
{
if (myXMLReader.Name == "photo")
{
double tempPrice = 0.0;
double.TryParse(myXMLReader.GetAttribute("price"), out tempPrice);
returnValue.Add(new Item(
myXMLReader.GetAttribute("info"),
tempPrice,
ImagesRootFolder + myXMLReader.GetAttribute("image"),
myXMLReader.GetAttribute("sku")
));
}
}
}
}
catch { }
//Done
ItemList = returnValue;
IsReadingXML = false;
}
}
感謝您對嵌入式文件文章的鏈接,我使用在線文件,您可以在下面看到我的答案。乾杯! – MattyMerrix
@MattyMerrix如果我的文章幫助你,請務必點擊投票上傳/回覆。 –