linq-to-xml

How to use a LINQ query to get XElement values when XElements have same name

北城余情 提交于 2019-12-18 04:48:13
问题 I have a piece of xml like the following: <Table> <Record> <Field>Value1_1</Field> <Field>Value1_2</Field> </Record> <Record> <Field>Value2_1</Field> <Field>Value2_2</Field> </Record> </Table> What i would like is a LINQ query that generates an IEnumerable that i can assign as the datasource of a DataGrid. What i have so far is as follows: var temp = from record in table.Elements("Record") select record.Element("Field").Value The fact that I can have multiple field elements is my stumbling

Using Linq and XDocument, can I get all the child elements under parent tag?

前提是你 提交于 2019-12-18 04:45:18
问题 I have an XML <data> <summary> <account curr_desc='USD' acct_nbr='123' net='1000.00' /> <account curr_desc='USD' acct_nbr='456' net='2000.00' /> </summary> <details> <accounts> <account acct_nbr="123" curr="USD"> <activity color='False' settle_date='02 Jul 2010' amt='580.00' /> <activity color='True' settle_date='09 Jul 2010' amt='420.00' /> </account> <account acct_nbr="456" curr="USD"> <activity color='True' settle_date='12 Dec 2010' amt='1500.00' /> <activity color='True' settle_date='19

Automapper to create object from XML

假如想象 提交于 2019-12-18 04:18:13
问题 If I have the following class: class SPUser { public int ID { get; set; } public string Name { get; set; } public string LoginName { get; set; } public string Email { get; set; } public bool IsSiteAdmin { get; set; } public bool IsSiteAuditor { get; set; } public bool IsDomainGroup { get; set; } public List<SPGroup> Groups { get; set; } } And I am using the sharepoint web services, which return an XML with an attribute for each property on my class, such as: <Users> <User Name="name"

Creating XDocument with xsi:schemaLocation namespace

不羁岁月 提交于 2019-12-18 03:54:59
问题 I need to create the following XML and I'm trying to do this using XDocument. However, I'm having trouble specifying the name spaces. <AssessmentOrderRequest xsi:schemaLocation="http://ns.hr-xml.org/2007-04-15 http://ns.hr-xml.org/2_5/HR-XML-2_5/StandAlone/AssessmentOrderRequest.xsd" xmlns="http://ns.hr-xml.org/2007-04-15" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </AssessmentOrderRequest> This is the sort of code that I'm looking for, however, I can't create attributes with a

Use XDocument as the source for XmlSerializer.Deserialize?

北城以北 提交于 2019-12-17 22:18:21
问题 I would like to invoke XmlSerializer.Deserialize passing it an XDocument . It can take a Stream , an XmlReader or a TextReader . Can I generate one of the above from XDocument without actually dumping the XDocument into some intermediate store, such as a MemoryStream ? It seems that what I'm after is an implementation of XmlReader that works with an XDocument . I can't find one though. 回答1: You can use XDocument.CreateReader() to create an XmlReader that reads the contents of the XDocument .

Select element with given attribute using linq to xml

寵の児 提交于 2019-12-17 20:14:13
问题 I've got following XML structure: <artists> <artist> <name></name> <image size="small"></image> <image size="big"></image> </artist> </artists> I need to select name and image with given attribute (size = big). var q = from c in feed.Descendants("artist") select new { name = c.Element("name").Value, imgUrl = c.Element("image").Value }; how can I specify needed image attribute(size=big) in query above? 回答1: It's quite simple when you know how! var artistsAndImage = from a in feed.Descendants(

How do I insert an element into XML using Linq?

半腔热情 提交于 2019-12-17 19:40:39
问题 My XML: <content> <item id="1">A</item> <item id="2">B</item> <item id="4">D</item> </content> I have loaded this using XML similar to: XDocument xDoc = new XDocument(data.Value); var items = from i in xDoc.Element("content").Elements("item") select i; I want to insert another element, to end up with something like: <content> <item id="1">A</item> <item id="2">B</item> <item id="3">C</item> <item id="4">D</item> </content> How do I do this using Linq2Xml? 回答1: Try this: xDoc.Element("content"

Weirdness with XDocument, XPath and namespaces

余生长醉 提交于 2019-12-17 18:38:36
问题 I have an XML document that looks like this: <kmsg xmlns="http://url1" xmlns:env="url1" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:schemaLocation="http://location that does not exist.xsd"> <header> <env:envelope> <env:source branch="907" machine="0" password="J123"/> </env:envelope> </header> <body> <OrderResponse xmlns="urn:schemasbasdaorg:2000:orderResponse:xdr:3.01"> <SomeMoreNodes/> </OrderResponse> </body> It does not have any schemas available despite having namespaces

Parent/Children Xml to DTO Object Model with LINQ

…衆ロ難τιáo~ 提交于 2019-12-17 17:23:40
问题 Given the below DTO definitions: [Serializable] internal class OrderCollection : List<Order> { } [Serializable] internal class Order { public string OrderId { get; set; } public OrderDetailCollection OrderDetails { get; set; } } [Serializable] internal class OrderDetailCollection : List<OrderDetail> { } [Serializable] internal class OrderDetail { internal OrderDetail() { } /*public string ParentOrderId { get; set; }*/ public string ItemName { get; set; } public int Quantity { get; set; } }

Query XDocument with xmlns attribute (namespace)

非 Y 不嫁゛ 提交于 2019-12-17 16:32:57
问题 I try to query elements from an visual studio *.csproj file. I created a short example to illustrate the problem: // Working string xml1 = @"<Project ToolsVersion='4.0'> <ItemGroup Label='Usings'> <Reference Include='System' /> <Reference Include='System.Xml' /> </ItemGroup> </Project>"; // Not working string xml2 = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> <ItemGroup Label='Usings'> <Reference Include='System' /> <Reference Include='System.Xml