Tag: linq to xml

如何通过LINQ to XML创建Dictionary ?

我有以下XML: 我有以下代码,我将得到一个Dictionary()对象 myObject.FootNotes 这样每条线都是一个键/值对 var doc = XElement.Parse(xmlString); var myObject = new { FootNotes = (from fn in doc .Elements(“FootNotes”) .Elements(“Line”) .ToDictionary ( column => (int) column.Attribute(“id”), column => (string) column.Attribute(“reference”) ) ) }; 我不确定如何将XML从XML转换为对象。 有谁能提出解决方案?

System.Collections.Generic.KeyNotFoundException:给定的键不在字典中

我在对方法执行unit testing时收到上述错误消息。 我知道问题出在哪里,我只是不知道为什么它不在字典中。 这是字典: var nmDict = xelem.Descendants(plantNS + “Month”).ToDictionary( k => new Tuple(int.Parse(k.Ancestors(plantNS + “Year”).First().Attribute(“Year”).Value), Int32.Parse(k.Attribute(“Month1”).Value), k.Ancestors(plantNS + “Report”).First().Attribute(“Location”).Value.ToString()), v => { var detail = v.Descendants(plantNS + “Details”).First(); return new HoursContainer { BaseHours = detail.Attribute(“BaseHours”).Value, OvertimeHours = detail.Attribute(“OvertimeHours”).Value, TotalHours = float.Parse(detail.Attribute(“BaseHours”).Value) + float.Parse(detail.Attribute(“OvertimeHours”).Value) }; }); var mergedDict = new Dictionary<Tuple, HoursContainer>(); foreach (var item […]

XDocument.Descendants(itemName) – 查找限定名称的问题

我正在尝试从网站上阅读XML-RSS-Feed。 因此,我使用异步下载并使用XDocument.Parse()方法创建XDocument 。 该文件旨在非常简单,如下所示: … … 现在我想读出所有的项目。 因此我试过: foreach (XElement NewsEntry in xDocument.Descendants(“item”)) 但这不起作用。 所以我在这个板上发现了一个使用限定名称的post,因为在根元素中定义了一些名称空间: 好吧,我尝试了所有3个可用的名称空间 – 没有任何对我有用: XName itemName = XName.Get(“item”, “http://www.w3.org/1999/02/22-rdf-syntax-ns#”); XName itemName2 = XName.Get(“item”, “http://purl.org/dc/elements/1.1/”); XName itemName3 = XName.Get(“item”, “http://purl.org/rss/1.0/modules/syndication/”); 任何帮助,将不胜感激。 (通常我正在使用Regex进行XML分析 – 但这次我正在为移动设备开发,因此需要关注性能。)

使用C#将XDocument或XmlDocument转换为JSON

我有这个XML很棒: 但它输出到这个JSON: {“Products”:{“Product”:[{“@ProductCode”:”C1010″,”@CategoryName”:”Coins”}, {“@ProductCode”:”C1012″,”@CategoryName”:”Coins”}, {“@ProductCode”:”C1013″,”@CategoryName”:”Coins”}]}} 我想在我的json中没有’产品’分级,因为所有三行都是产品。 这是我的C#代码: //x is an XDocument. JsonConvert.SerializeXNode(x, Formatting.None, false) //JsonConvert.SerializeXNode(x); //I also tried without the formatting and the boolean. 当我’将’XDocument’转换为XmlDocument并使用: var xmlDocument = new System.Xml.XmlDocument(); using (var xmlReader = x.CreateReader()) { xmlDocument.Load(xmlReader); } JsonConvert.SerializeXmNode(xmlDocument); 它给了我完全相同的输出。 那么如何修改我的JSON解析,以便我有一个简单的产品列表。 我更喜欢最干净的解决方案。 或许更清楚一点,我这样的东西作为输出: [{“@ProductCode”:”C1010″,”@CategoryName”:”Coins”}, {“@ProductCode”:”C1012″,”@CategoryName”:”Coins”}, {“@ProductCode”:”C1013″,”@CategoryName”:”Coins”}]

Linq到Xml,保持XDocument加载?

假设我正在制作一个WinForms程序,它将在幕后使用XML文档作为持久性机制…… 以下两种方法的优缺点是什么? 在每个方法调用中加载XDocument: public class XmlFoosRepository { string xmlFileName; public XmlFoosRepository(string xmlFileName) { this.xmlFileName = xmlFileName; } public int AddFoo(Foo foo) { var xDoc = XDocument.Load(xmlFileName); // Always call Load() // … xDoc.Save(xmlFileName); return foo.ID; } public IEnumerable<Foo> GetFoos() { var xDoc = XDocument.Load(xmlFileName); // Always call Load() // … return foos; } } 要么 […]

读取和使用XML文件作为数据库 – Windows Phone 7应用程序

我有一个应用程序,它应该读取xml文件中的数据,然后使用该数据。 如何在我的应用程序中导入xml文件(代码是什么)以及如何使用该xml文件中的数据? 这是我使用的xml数据库的一个例子: Ant Aardwark Bear Boa 我也尝试过这个 XDocument loadedData = XDocument.Load(“Data.xml”); 从xml文件中读取数据但不起作用。 我还能以什么forms使用xml数据? 换句话说,xml数据将采用字符串格式或“X-Something”格式? 更新:也许Xml反序列化对我有用吗? 先感谢您

使用linq到xml,使用单引号保存xml元素的属性值

如何使XDocument对象使用单引号保存元素的属性值?

使用ASP.net和VB.net(或C#)从XML文件编辑和删除数据

在我的Web应用程序中 ,我有一个名为“answers.xml”的XML文件 它以XML格式存储用户条目 4444 test patient blah blah xxxx Negative 1 1 1 1 Positive dx 我可以使用从互联网上下载的DLL文件将数据添加到XML文件中。 我需要一种方法来改变使用ASP.net / VB.net或C#的xml文件中的数据(编辑/删除)

System.IO.IOException错误,无法访问该文件

我无法理解问题在哪里,尽管事实上,这个代码非常简单。 我有这样的function: public void WriteToDoc(string path) { XDocument doc = new XDocument(new XElement(“General parameters”, new XElement(“num_path”, num_path.Text), new XElement(“Gen_Peroid”, Gen_Peroid.Text), new XElement(“Alg_Perioad”, Alg_Perioad.Text)) ); doc.Save(path); // here he gives that exception } num_path.Text , Gen_Peroid.Text和Alg_Perioad.Text是string 。 这是我使用此function的方式: File.Create(@”C:\ProgramData\RadiolocationQ\Q.xml”); WriteToDoc(@”C:\ProgramData\RadiolocationQ\Q.xml”); 它创建文件,但该文件中没有写入任何内容。 所以确切的错误System.IO.IOException错误,进程无法访问该文件,因为它正被另一个进程使用。 怎么可能得到这样的错误?

如何在窗口手机7中加载位于应用程序文件夹内的XML文件?

我正在开发windows phone 7应用程序。 我是窗口手机7应用程序的新手。 我通过右键单击项目并选择Add – > New Item在我的项目中添加了XML文件。 然后,我可以使用以下代码轻松地在我的应用程序中加载XML文件 IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication(); XDocument doc = null; IsolatedStorageFileStream isfStream = null; if (isfData.FileExists(strXMLFile)) { isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData); doc = XDocument.Load(isfStream); isfStream.Close(); } else { doc = XDocument.Load(strXMLFile); isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData); doc.Save(isfStream); isfStream.Close(); } 通过使用上面的代码,我可以在我的XML文件中执行读写操作。 但是当我将XML文件放入文件夹时出现了问题。 我的问题如下:通过右键单击项目名称并在visual studio中选择Add – […]