public string GetWeatherData() { try { string url = weatherLocation + zipCode; XmlDocument doc = new XmlDocument(); string cachedXml = Services.Get<ICache>().Get(url) as string ?? string.Empty; if (string.IsNullOrEmpty(cachedXml)) doc.Load(url); else doc.LoadXml(cachedXml); if (null == Services.Get<ICache>().Get(url)) Services.Get<ICache>().Add(url, doc.ToXml()); XmlElement root = doc.DocumentElement; XmlNodeList nodes = root.SelectNodes("/rss/channel/item"); string data = ""; foreach (XmlNode node in nodes) { data = data + node["title"].InnerText; data = data + node["description"].InnerText; } return data; } catch { return string.Empty; } }
/// <summary>Saves this XmlDocument to a file. You can just use XmlDocument.Save, but this setup up indentation and whatnot.</summary> /// <remarks> /// Right now this is stupid and inefficient ... we just write doc.ToXml() to our file. But ... meh ... it works. /// </remarks> public static void SaveToFile(this XmlDocument doc, string path) { if (doc == null) { return; } using (var writer = new StreamWriter(path)) writer.Write(doc.ToXml()); }
public void TestXmlDocumentSeiralization() { var objTree = new ObjectTree { Foo = "Foo1", Bar = 1, Nested = new List <object>() }; objTree.Nested.AddRange(new[] { new ObjectTree { Foo = "Foo2", Bar = 2 }, new ObjectTree { Foo = "Foo3", Bar = 3 }, }); XmlDocument xmlDoc = objTree.ToXmlDocument(); string xml = xmlDoc.ToXml(); Assert.IsNotNull(xml); XmlNode parentElem = xmlDoc.DocumentElement; objTree.SerializeInto(parentElem); xml = xmlDoc.ToXml(); Assert.IsNotNull(xml); xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); ObjectTree newObjectTree = xmlDoc.Deserialize <ObjectTree>(); Assert.IsNotNull(newObjectTree); ObjectTree nested = newObjectTree.AnyXml.Deserialize <ObjectTree>(); Assert.AreEqual(1, nested.Bar); Assert.AreEqual(objTree.Nested.Count, nested.Nested.Count); }
public void Add(TModel model) { var xdoc = new XmlDocument(); xdoc.Load(FileName); XmlNode xnode = xdoc.ToXml(model); xdoc.DocumentElement.AppendChild(xnode); xdoc.Save(FileName); }
public void AddRange(IEnumerable <TModel> models) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(FileName); foreach (var model in models) { XmlNode xnode = xdoc.ToXml(model); xdoc.DocumentElement.AppendChild(xnode); } xdoc.Save(FileName); }
public string GetHoroscope() { try { SetHoroscopeData(); string imgName = ddlHoroscope.SelectedItem.Text.ToLower() + ".gif"; string data = ""; data = "<img src='Widgets/Horoscope_image/" + imgName + "'/><br/><b> " + strHoroscope[ddlHoroscope.SelectedIndex] + "</b>"; XmlDocument doc = new XmlDocument(); string cachedXml = Services.Get <ICache>().Get(rssLocation) as string ?? string.Empty; try { if (string.IsNullOrEmpty(cachedXml)) { doc.Load(rssLocation); } else { doc.LoadXml(cachedXml); } } catch { return(string.Empty); } if (null == Services.Get <ICache>().Get(rssLocation)) { Services.Get <ICache>().Add(rssLocation, doc.ToXml()); } XmlElement root = doc.DocumentElement; XmlNodeList nodes = root.SelectNodes("/rss/channel/item"); foreach (XmlNode node in nodes) { string str = node["title"].InnerText; if (str.StartsWith(ddlHoroscope.SelectedItem.Text)) { data = data + node["description"].InnerText; data = data.Remove(data.IndexOf("More horoscopes!")); } } return(data); } catch { return(string.Empty); } }
public string GetHoroscope() { try { SetHoroscopeData(); string imgName = ddlHoroscope.SelectedItem.Text.ToLower() + ".gif"; string data = ""; data = "<img src='Widgets/Horoscope_image/" + imgName + "'/><br/><b> " + strHoroscope[ddlHoroscope.SelectedIndex] + "</b>"; XmlDocument doc = new XmlDocument(); string cachedXml = Services.Get<ICache>().Get(rssLocation) as string ?? string.Empty; try { if (string.IsNullOrEmpty(cachedXml)) doc.Load(rssLocation); else doc.LoadXml(cachedXml); } catch { return string.Empty; } if (null == Services.Get<ICache>().Get(rssLocation)) Services.Get<ICache>().Add(rssLocation, doc.ToXml()); XmlElement root = doc.DocumentElement; XmlNodeList nodes = root.SelectNodes("/rss/channel/item"); foreach (XmlNode node in nodes) { string str = node["title"].InnerText; if (str.StartsWith(ddlHoroscope.SelectedItem.Text)) { data = data + node["description"].InnerText; data = data.Remove(data.IndexOf("More horoscopes!")); } } return data; } catch { return string.Empty; } }
public string GetWeatherData() { try { string url = weatherLocation + zipCode; XmlDocument doc = new XmlDocument(); string cachedXml = Services.Get <ICache>().Get(url) as string ?? string.Empty; if (string.IsNullOrEmpty(cachedXml)) { doc.Load(url); } else { doc.LoadXml(cachedXml); } if (null == Services.Get <ICache>().Get(url)) { Services.Get <ICache>().Add(url, doc.ToXml()); } XmlElement root = doc.DocumentElement; XmlNodeList nodes = root.SelectNodes("/rss/channel/item"); string data = ""; foreach (XmlNode node in nodes) { data = data + node["title"].InnerText; data = data + node["description"].InnerText; } return(data); } catch { return(string.Empty); } }
public static dynamic ToObject(this XmlDocument x) { return(Instance.FromXml(x.ToXml(Encoding.ASCII))); }
/// <summary>The most common option that we want to toggle is indentation. This lets you do that easily.</summary> public static string ToXml(this XmlDocument doc, bool indent) { return(doc.ToXml(new XmlWriterSettings { Indent = true })); }
/// <summary>Writes the current XmlDocument out to a string using our default settings (eg. indented)</summary> public static string ToXml(this XmlDocument doc) { return(doc.ToXml(true)); }