linq-to-xml

LINQ to XML: creating complex anonymous type

情到浓时终转凉″ 提交于 2019-12-11 07:27:41
问题 I've an xml file as follows: <ProductGroup> <Product id="4601A"> <name>Roses</name> <section>Floral</section> <price>46</price> <PopupImages> <PopupImage>img1.jpg</PopupImage> <PopupImage>img2.jpg</PopupImage> </PopupImages> <ImageThumbs> <thumb>img1-thm.jpg</thumb> <thumb>img2-thm.jpg</thumb> </ImageThumbs> </Product> </ProductGroup> In production the ProductGroup node might contain many Product nodes. For this I kind of want to build a list of an anonymous object that has the following

Linq to XML: get default namespace of rss

喜你入骨 提交于 2019-12-11 07:26:37
问题 I call the methods within several classes this way: GetResult(XElement item, XNamespace ns) { item.Element(ns + "title").Value; } In order to initialize the feeds and to access the elements as stated above I want to find out the default namespace. Without any namespace declaration it works fine (item.Element("title").Value) and the value of the element is returned. So how can I find out the correct namespace? The result of the method root.GetDefaultNamespace() is empty somehow... <rss xmlns

Linq to XML Namespace problems

一世执手 提交于 2019-12-11 07:14:05
问题 Greetings, My goal: To validate an XML document then load the data into a custom object. I'm using Linq to XML. My situation: I'm struggling with the Namespace and/or the Linq syntax. I thought everything was working. The code reads the XML and loads the object, but realized the XDocument.Validate was passing everything through and not really validating. I guess that's lax validation. To get the XDocument.Validate() method to validate, I added a Namespace to the XML file. The Validation works

How to keep in session XML-based objects?

五迷三道 提交于 2019-12-11 07:07:26
问题 I'm receiving data from web-services in XML and I'm using that data via objects, build upon received XML. So, sometimes I need to store such user-specific objects between requests in session. I know that XMLDocument couldn't be stored explicitly (state server)... so I'm making a terrible construction like: private string _data; public XmlDocument Data { get { XmlDocument res = new XmlDocument(); if (!string.IsNullOrEmpty(_data)) { res.InnerXml = _data; return res; } return null; } set { _data

VB.Net, Linq to Xml: Is this the correct way to use logic while creating xml?

你说的曾经没有我的故事 提交于 2019-12-11 06:59:34
问题 While building an xml document I require to use logic to dictate the outcome of the xml; logically it is similar to the following piece of code (although this does not work): Dim buildElement As Boolean = True Dim xe As XElement = _ <xml> <% If buildElement Then %> <BuildMyElement><%= buildElement.ToString %></BuildMyElement> <% End If %> </xml> I have managed to do this using the method show below, is this the suggested way of doing this or is there a better one?? Dim buildElement As Boolean

Get the Count of Elements where candidate has won

感情迁移 提交于 2019-12-11 06:13:23
问题 LINQ2Xml: I would like to get the count of elements where candidate has won in every province. I need some help. <Pronvice_Data> <Pronvice>PronviceA</Pronvice> <Registered_Voters>115852</Registered_Voters> <Sam_Kea>100</Sam_Kea> <Jeje>500</Jeje> <John_Doe>400</John_Doe> </Pronvice_Data> <Pronvice_Data> <Pronvice>PronviceA</Pronvice> <Registered_Voters>25852</Registered_Voters> <Sam_Kea>200</Sam_Kea> <Jeje>100</Jeje> <John_Doe>300</John_Doe> </Pronvice_Data> <Pronvice_Data> <Pronvice>PronviceC

IntelliSense for XElement objects with XML schema

ぃ、小莉子 提交于 2019-12-11 05:29:31
问题 Reading an article called "Increase LINQ Query Performance" in July's MSDN magazine, the author states that using an Imports in VB providing a path to schema in the current project will turn IntelliSense on for XElement. In the code provided, he uses statements like xelement.@name to retreive attributes values and so on. I did not try this out myself in VB but I would like to use that in C#. This really looks like LINQ to XSD. Is there any equivalent in C#? It seems that it is not possible to

Get XML attribute with C#

↘锁芯ラ 提交于 2019-12-11 05:26:09
问题 I have an XML file like the following. <div class="time"> <span class="title">Bla: </span> <span class="value">Thu 20 Jan 11</span> </div> How can I get value "Thu 20 Jan 11" with C#? thanks in advance 回答1: Sounds like you rather need an HTML Parser IMHO. if so, then Take a look at Html Agility Pack 回答2: Given that you do have an XML file like you say, then you need could load the file into an XmlDocument and find what you want using XPath: class Program { static void Main(string[] args) {

LINQ to XML Sum Child Nodes in VB.NET

家住魔仙堡 提交于 2019-12-11 05:14:40
问题 I have the following XML from Amazon's Marketplace API. I need to sum all the values of Item/ItemPrice/Component[type='Principal']/Amount for all Items to compute an order total. Is this possible to do using LINQ to XML in VB.NET? <?xml version="1.0"?> <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd"> <Header> <DocumentVersion>1.01</DocumentVersion> <MerchantIdentifier>My Store</MerchantIdentifier> </Header> <MessageType

Using LINQ to XML to traverse an HTML table

坚强是说给别人听的谎言 提交于 2019-12-11 04:44:30
问题 So, I can easily use LINQ to XML to traverse a properly set-up XML document. But I'm having some issues figuring out how to apply it to an HTML table. Here is the setup: <table class='inner' width='100%'> <tr> <th>Area</th> <th>Date</th> <th>ID</th> <th>Name</th> <th>Email</th> <th>Zip Code</th> <th>Type</th> <th>Amount</th> </tr> <tr> <td>Data</td> <td>Data</td> <td>Data</td> <td>Data</td> <td>Data</td> <td>Data</td> <td>Data</td> <td>Data</td> </tr> <tr> <td>Data</td> <td>Data</td> <td>Data