linq-to-xml

Read a XML tree structure recursively in a List<T> with children lists<T>

感情迁移 提交于 2019-12-20 12:36:34
问题 I have an XML like this: And I have a Member class with property Name. How can I read every Unit and its children Units into multiple generic List<Unit> which can have again children List<Unit> in a recursively way using latest .NET technology ? <Root> <Units Name="Test1"> <Unit Name="Test11" /> <Unit Name="Test12"> <Unit Name="Test21" /> <Unit Name="Test22" /> <Unit Name="Test23"> <Unit Name="Test31" /> <Unit Name="Test32" /> <Unit Name="Test33" /> </Unit> <Unit Name="Test24" /> </Unit> <

Xml Element not present and list null and Object reference not set to an instance of an object

可紊 提交于 2019-12-20 07:10:10
问题 I am retireving data from xml field, there are nodes that may not exist (because the xml file is generated dynamically) . the problem is when I look for a node and it is not present in the file , it returns a null list and this exception :"d Object reference not set to an instance of an object ". Here is the code : public static List<Transaction> getXmlTransactions(XElement n) { var transactions = n.Elements("Transaktion").Select(p => new Transaction() { TransID = p.Element("TransID") != null

Get value from xml element in c#

北城以北 提交于 2019-12-20 05:53:07
问题 I am trying to get Absoluteentry tag's value from the below xml string, but its displaying objectrefrence not set exception <?xml version="1.0" ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Body> <AddResponse xmlns="http://www.sap.com/SBO/DIS"> <PickListParams> <Absoluteentry>120072</Absoluteentry> </PickListParams> </AddResponse> </env:Body> </env:Envelope> Code XDocument doc = XDocument.Parse(xmlstring); doc.Element("Envelope").Element("Body").Element(

Special characters with XDocument

不羁岁月 提交于 2019-12-20 05:37:06
问题 I'm trying to read a file (not a XML, but the structure is similar), but i'm getting this Exception: '┴', hexadecimal value 0x15, is an invalid character. Line 8, position 7. and the file have a lot of this symbols, that I can't replace because I can't modify the content of the file for my purposes... That's the code: try { XDocument doc = new XDocument(new XDeclaration("1.0", "utf-16", "yes")); doc = XDocument.Load(arquivo); } catch (Exception e) { MessageBox.Show(e.Message.ToString()); }

Dynamic Linq to Xml example

末鹿安然 提交于 2019-12-20 05:35:13
问题 I need a basic example on how to use System.Linq.Dynamic with Xml. Here’s a functioning statement I want to convert to dynamic Linq: XElement e = XElement.Load(new XmlNodeReader(XmlDoc)); var results = from r in e.Elements("TABLES").Descendants("AGREEMENT") where (string)r.Element("AGRMNT_TYPE_CODE") == "ISDA" select r.Element("DATE_SIGNED"); foreach (var x in results) { result = x.Value; break; } Here’s the approach I am using: string whereClause = "(\"AGRMNT_TYPE_CODE\") == \"ISDA\"";

Object reference not set to an instance of an object. Trying to put XML into a List

浪子不回头ぞ 提交于 2019-12-20 04:16:58
问题 I have to following XML code witch I like to convert into a List with keys and values: <?xml version='1.0' encoding='UTF-8' standalone='no'?> <root> <command>getClient</command> <id>10292</id> </root> My C# code is like this: XElement aValues = XElement.Parse(sMessage); List<KeyValuePair<string, object>> oValues = aValues.Element("root").Elements().Select(e => new KeyValuePair<string, object>(e.Name.ToString(), e.Value)).ToList(); sMessage is the XML string. Now I'm getting the following

XML Parsing Error: not well-formed ? anyone can

时间秒杀一切 提交于 2019-12-20 03:10:36
问题 This is my first time creating a xml document but I am having problems with one line not working. can anyone tell me what I have done wrong. http://www.ncerttext.in/flipkart.xml error is 7: 107 The entity "wgtid" was referenced, but not declared. how should i do that. 回答1: As suggested by @Marco Forberg, the problematic character in your XML is "&" . Simply change & to & . By the way: Modern browsers tell you something about XML files even if they are malformed. For example, if you open your

Delete an inner node but not the value in xml with XDocument library in C# .NET

…衆ロ難τιáo~ 提交于 2019-12-20 03:09:02
问题 I have the following XML file: <?xml version="1.0" encoding="utf-8"?> <html> <body> <p><span class="screenitems">Add</span></p> </body> </html> I Want it to delete <span> node but the Add to exist, so it looks as follows at the end: <?xml version="1.0" encoding="utf-8"?> <html> <body> <p>Add</p> </body> </html> Is there a good/simple way to do this? 回答1: Use ReplaceWith , e.g. XDocument doc = XDocument.Load("file.xml"); XElement span = doc.Descendants("p").First().Elements("span")

How I use XDocument.Load() to get a file in the application directory

我与影子孤独终老i 提交于 2019-12-20 01:44:33
问题 I have a XML file in a folder in the project. I would like to use XDocument.Load(string) but do I have to write the complete path to the application as part of the URI (such as in my example)? XDocument xml = XDocument.Load("c:/users/myuser/documents/visual studio 2010/Projects/ErrorRegistro/Registro.xml"); 回答1: Another way that bypass the ressource issue is to add the file to project (same as the ressource solution), then click on the file and in the property tab choose "copy always" in copy

XDocument : is it possible to force the load of a malformed XML file?

不羁的心 提交于 2019-12-19 20:00:20
问题 I have a malformed XML file. The root tag is not closed by a tag. The final tag is missing. When I try to load my malformed XML file in C# StreamReader sr = new StreamReader(path); batchFile = XDocument.Load(sr); // Exception I get an exception " Unexpected end of file has occurred. The following elements are not closed: batch. Line 54, position 1. " Is it possible to ignore the close tag or to force the loading? I noticed that all my XML tools ((like XML notepad) ) automaticly fix or ignore