本文整理汇总了C#中System.Xml.Linq.XDocument.Dictionary方法的典型用法代码示例。如果您正苦于以下问题:C# XDocument.Dictionary方法的具体用法?C# XDocument.Dictionary怎么用?C# XDocument.Dictionary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XDocument
的用法示例。
在下文中一共展示了XDocument.Dictionary方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: dictionary_xml_reader
public void dictionary_xml_reader()
{
Assert.Throws<ArgumentNullException>(() => ((XmlReader)null).Dictionary());
var xml = new XDocument(
new XElement("Articles",
new XElement("Article",
new XComment("Comment"),
new XAttribute("Id", "id"),
new XElement("Name", "name"),
new XElement("Date", DateTime.MaxValue),
new XElement("Description", new XCData("description")),
new XElement("Tags",
new XElement("Tag", "tag1"),
new XElement("Tag", "tag2")))));
var xmlDictionary = xml.Dictionary();
IDictionary<string, object> dictionary;
using (var reader = new StringReader(xml.ToString()).XmlReader(true))
{
dictionary = reader.Dictionary();
Assert.True(dictionary.Keys.SequenceEqual(xmlDictionary.Keys));
}
var xmlReader = new StringReader(xml.ToString()).XmlReader(true);
dictionary = xmlReader.Dictionary();
Assert.False(xmlReader.Read());
Assert.True(dictionary.Keys.SequenceEqual(xmlDictionary.Keys));
}
示例2: dictionary_x_document
public void dictionary_x_document()
{
Assert.Throws<ArgumentNullException>(() => ((XDocument)null).Dictionary());
var xml = new XDocument(
new XElement("Articles",
new XElement("Article",
new XComment("Comment"),
new XAttribute("Id", "id"),
new XElement("Name", "name"),
new XElement("Date", DateTime.MaxValue),
new XElement("Description", new XCData("description")),
new XElement("Notes", string.Empty),
new XElement("Tags",
new XElement("Tag", "tag1"),
new XElement("Tag", "tag2")))));
var dictionary = xml.Dictionary();
Assert.Equal(1, dictionary.Keys.Count);
Assert.True(dictionary.ContainsKey("Articles"));
var article = dictionary["Articles"].To<IDictionary<string, object>>()["Article"].To<IDictionary<string, object>>();
Assert.Equal(6, article.Keys.Count);
Assert.False(article.ContainsKey("Comment"));
Assert.Equal("id", article["Id"].ToString());
Assert.Equal("name", article["Name"].ToString());
Assert.Equal(DateTime.MaxValue.RFC1121(), article["Date"].ToString().ToDateTime().RFC1121());
Assert.Equal("description", article["Description"].ToString());
Assert.Null(article["Notes"]);
var tags = article["Tags"].To<IDictionary<string, object>>();
Assert.Equal(1, tags.Keys.Count);
Assert.Equal("tag2", tags["Tag"].ToString());
}