linq-to-xml

How do I find a XML node by path in Linq-to-XML

丶灬走出姿态 提交于 2019-12-19 06:46:09
问题 If I get the path to a specific node as a string can I somehow easily find said node by using Linq/Method of the XElement ( or XDocument ). There are so many different types of XML objects it would also be nice if as a added bonus you could point me to a guide on why/how to use different types. EDIT: Ok after being pointed towards XPathSelectElement I'm trying it out so I can give him the right answer I can't quite get it to work though. This is the XML I'm trying out <Product> <Name>SomeName

Forcing XDocument.ToString() to include the closing tag when there is no data

那年仲夏 提交于 2019-12-19 05:36:11
问题 I have a XDocument that looks like this: XDocument outputDocument = new XDocument( new XElement("Document", new XElement("Stuff") ) ); That when I call outputDocument.ToString() Outputs to this: <Document> <Stuff /> </Document> But I want it to look like this: <Document> <Stuff> </Stuff> </Document> I realize the first one is correct, but I am required to output it this way. Any suggestions? 回答1: Set the Value property of each empty XElement specifically to an empty string. // Note: This will

What does the “New … With” syntax do in VB Linq?

房东的猫 提交于 2019-12-19 05:06:58
问题 What (if any) is the difference between the results of the following two versions of this VB Linq query? ' assume we have an XElement containing employee details defined somewhere else Dim ee = From e In someXML.<Employee> _ Select New With {.Surname = e.<Surname>, .Forename = e.<Forename>} and Dim ee = From e In someXML.<Employee> _ Select Surname = .Surname = e.<Surname>, .Forename = e.<Forename> ie what is the point of the New ... With syntax? I suspect that this has a simple answer, but I

LINQ: Remove Element from XML based on attribute value?

假如想象 提交于 2019-12-19 02:46:06
问题 How can I remove any element in xml based on matched attribute value? Here is my XML : <Projects> <Project serverUrl="tcp://xyz1:xxx/sdfsdf.rem" projectName="project1" /> <Project serverUrl="tcp://xyz2:xxx/sdfsdf.rem" projectName="project2" /> <Project serverUrl="tcp://xyz3:xxx/sdfsdf.rem" projectName="project3" /> <Project serverUrl="tcp://xyz4:xxx/sdfsdf.rem" projectName="project4" /> <Project serverUrl="tcp://xyz5:xxx/sdfsdf.rem" projectName="project5" /> <Project serverUrl="tcp://xyz6:xxx

Can't use Descendants() or Elements() with xmlns

放肆的年华 提交于 2019-12-18 20:49:28
问题 I'm new to working with XML, and I've encountered a weird problem while trying to get a specific tag from a spring.net configuration file. After trying to narrow down the problem with a test xml file, I found out that applying the following code: List<XElement> nodes = xmlFile.Descendants("B").ToList(); provides a non-empty list with the following file: <?xml version="1.0" encoding="utf-8" ?> <A fakeAttribute="aaa"> <B id="DbProvider"/> </A> but provides an empty string with the following

Parsing XML String in C#

て烟熏妆下的殇ゞ 提交于 2019-12-18 19:09:16
问题 I have looked over other posts here on the same subject and searched Google but I am extremely new to C# NET and at a loss. I am trying to parse this XML... <whmcsapi version="4.1.2"> <action>getstaffonline</action> <result>success</result> <totalresults>1</totalresults> <staffonline> <staff> <adminusername>Admin</adminusername> <logintime>2010-03-03 18:29:12</logintime> <ipaddress>127.0.0.1</ipaddress> <lastvisit>2010-03-03 18:30:43</lastvisit> </staff> </staffonline> </whmcsapi> using this

LINQ to XML: How to select the next element

筅森魡賤 提交于 2019-12-18 18:58:09
问题 I have a plist file from an iPhone app. It looks like this below: <plist version="1.0"> <dict> <key>barcodes</key> <array> <string>JF893J89FJ-66666</string> <string>JF893J89FJ-55555</string> </array> <key>currentStep</key> <integer>1</integer> <key>dateFinished</key> <date>2010-05-10T18:33:25Z</date> <key>dateStarted</key> <date>2010-05-10T18:33:25Z</date> <key>description</key> <string>TEST</string> <key>geoRequired</key> <string>N</string> <key>inProgress</key> <string>N</string> <key>jobID

How to convert XML to Dictionary

人走茶凉 提交于 2019-12-18 18:48:15
问题 I've xml as following: <?xml version="1.0" encoding="UTF-8"?> <root> <data name="LogIn">Log In</data> <data name="Password">Password</data> </root> I success to do that without Linq, any one can help me to convert the following code to Linq: using (XmlReader reader = XmlReader.Create(_xml)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "data") { reader.MoveToAttribute("name"); string key = reader.Value; reader.MoveToContent(); string value =

XDocument.Descendants not returning descendants

允我心安 提交于 2019-12-18 18:35:57
问题 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SetNationalList xmlns="http://www.lge.com/ddc"> <nationalList> <portnumber>6000</portnumber> <slaveaddress>7000</slaveaddress> <flagzone>2</flagzone> <flagindivisual>5</flagindivisual> <flagdimming>3</flagdimming> <flagpattern>6</flagpattern> <flaggroup>9</flaggroup> </nationalList> </SetNationalList> </s:Body> </s

XDocument.Descendants not returning descendants

╄→尐↘猪︶ㄣ 提交于 2019-12-18 18:35:10
问题 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SetNationalList xmlns="http://www.lge.com/ddc"> <nationalList> <portnumber>6000</portnumber> <slaveaddress>7000</slaveaddress> <flagzone>2</flagzone> <flagindivisual>5</flagindivisual> <flagdimming>3</flagdimming> <flagpattern>6</flagpattern> <flaggroup>9</flaggroup> </nationalList> </SetNationalList> </s:Body> </s