linq-to-xml

ó not allowed in xml file but allowed in .net resource file?

独自空忆成欢 提交于 2019-12-22 11:15:29
问题 i'm parsing a few .net resource files (.resx). In that, i have this piece of data: información This works in my .net app, but when i try to load this file in my xml document XDocument xmlDoc = XDocument.Parse(s); i get this error: Reference to undeclared entity 'oacute'. Why is that? 回答1: ó is a named HTML entity that is not defined in XML. XML only defines a subset of the named HTML entities (namely & , &apos; , " , > and < if memory serves). You can use the numeric entity representation

How do I find an XML element by attribute using LINQ to XML?

三世轮回 提交于 2019-12-22 10:38:25
问题 I'm learning LINQ to XML and need to find the existence of an element with a particular attribute. At the moment I'm using: XElement groupCollectionXml = XElement.Parse(groupCollection.Xml); IEnumerable<XElement> groupFind = from vw in groupCollectionXml.Elements("Group") where (string) vw.Attribute("Name") == groupName select vw; if (groupFind.Count() == 0) return false; else return true; I know there is a more concise way of doing this, probably using Any(), but I'm not sure how to rewrite

Reading xml file from a resource

强颜欢笑 提交于 2019-12-22 05:23:11
问题 I am trying to load an xml file that is stored as a resource in my C# project so I can perform various LINQ queries. However at runtime an "Illegal characters in path" exception is thrown. This is how I am loading the file: XDocument doc = XDocument.Load(MyProject.Properties.Resources.XMLFile); 回答1: Wouldn't XMLFile here actually return the xml itself? If so: XDocument doc = XDocument.Parse(MyProject.Properties.Resources.XMLFile); 来源: https://stackoverflow.com/questions/337535/reading-xml

LINQ to XML - How to use XDocument the right way

时光总嘲笑我的痴心妄想 提交于 2019-12-22 04:52:52
问题 Now I will start off by saying this is indeed an assigment. I have however nearly finished it untill I ran into the Linq to XML syntax. I have 2 classes: Track and CD now as part of the assigment I create a cd and then added some tracks to it. After searching for lots of tutorials that explained perfectly how to go from xml to objects I just cannot seem to get this working (objects to xml). I currently have: //My list of cds List<CD> cds = new List<CD>(); //Make a new CD and add some tracks

LINQ to XML - How to use XDocument the right way

左心房为你撑大大i 提交于 2019-12-22 04:52:10
问题 Now I will start off by saying this is indeed an assigment. I have however nearly finished it untill I ran into the Linq to XML syntax. I have 2 classes: Track and CD now as part of the assigment I create a cd and then added some tracks to it. After searching for lots of tutorials that explained perfectly how to go from xml to objects I just cannot seem to get this working (objects to xml). I currently have: //My list of cds List<CD> cds = new List<CD>(); //Make a new CD and add some tracks

Convert single XElement to object

*爱你&永不变心* 提交于 2019-12-22 01:46:06
问题 I have a single XElement looking like this: <row flag="1" sect="" header="" body="" extrainfo="0" /> Then I have a class looking like this: public class ProductAttribute { public string Flag { get; set; } public string Sect { get; set; } public string Header { get; set; } public string Body { get; set; } public string Extrainfo { get; set; } } How can I convert this XElement into a ProductAttribute object? 回答1: You have to put the correct serialization attributes on your class and class

XDocument.Descendants() versus DescendantNodes()

亡梦爱人 提交于 2019-12-22 01:43:32
问题 I've looked at Nodes() vs DescendantNodes() usages? to see the difference between .Nodes() and .DescendantNodes() but what is the difference between: XDocument.Descendants() and XDocument.DescendantNodes()? var xmlDoc = XDocument.Load(@"c:\Projects\Fun\LINQ\LINQ\App.config"); var descendants = xmlDoc.Descendants(); var descendantNodes = xmlDoc.DescendantNodes(); foreach (var d in descendants) Console.WriteLine(d); foreach (var d in descendantNodes) Console.WriteLine(d); 回答1: Descendants

Disable XML validation when using XDocument

人走茶凉 提交于 2019-12-21 18:40:24
问题 I'm parsing an XLIFF document using the XDocument class. Does XDocument perform some validation of the content which I read into it, and if so - is there any way to disable that validation? I'm getting some weird errors if the XLIFF isn't valid XML (I don't care that it isn't, I just want to parse it). E.g. '.', hexadecimal value 0x00, is an invalid character. I'm currently reading the file like this: string FileLocation = @"C:\XLIFF\text.xlf"; XDocument doc = XDocument.Load(FileLocation);

Linq-to-XML query to select specific sub-element based on additional criteria

二次信任 提交于 2019-12-21 17:24:50
问题 My current LINQ query and example XML are below. What I'd like to do is select the primary email address from the email-addresses element into the User.Email property. The type element under the email-address element is set to primary when this is true. There may be more than one element under the email-addresses but only one will be marked primary. What is the simplest approach to take here? Current Linq Query (User.Email is currently empty): var users = from response in xdoc.Descendants(

Whats a Good Example to Write XML using VB.net 2008

◇◆丶佛笑我妖孽 提交于 2019-12-21 13:57:40
问题 Using this example how would I go about updating an XML file using this example: <foo> <n1> <s1></s1> <s2></s2> <s3></s3> </n1> <n1> <s1></s1> <s2></s2> <s3></s3> </n1> </foo> I Can read from it all day long but for the life of me I cannot seem to write it back into that format. 回答1: Straightforward approach: ' to create the XmlDocument... ' Dim xmlDoc As New Xml.XmlDocument Dim fooElement As Xml.XmlElement = xmlDoc.CreateElement("foo") xmlDoc.AppendChild(fooElement) Dim n1Element As Xml