Adding a XElement as a child of an exsisting XElement Linq
-
martes, 06 de marzo de 2012 15:26
I would like to add a XElement into an existing XElement as a child
Dim myDocument As New XDocument
myDocument.Declaration = New XDeclaration("1.0", "utf-8", "no")
myDocument.Add(New XComment("XComment"))
myDocument.AddAnnotation("XAddAnnotation")
Dim MyCustomer As New XElement("Customer", New XAttribute("Name", MyCust.Name), New XAttribute("EMail", MyCust.Email), New XAttribute("FirstName", MyCust.FirstName), _
New XAttribute("LastName", MyCust.LastName), New XAttribute("Salutation", MyCust.Salutation), _
New XAttribute("Company", MyCust.Company), New XAttribute("Address", MyCust.Address), New XAttribute("Address2", MyCust.Address2), _
New XAttribute("City", MyCust.City), New XAttribute("StateCode", MyCust.StateCode), New XAttribute("CountryISOCode", MyCust.CountryISOCode), _
New XAttribute("PostCode", MyCust.PostCode), New XAttribute("Phone", MyCust.Phone), New XAttribute("Fax", MyCust.Fax), _
New XAttribute("Comments", MyCust.Comments), _
New XElement("Purchase", New XAttribute("LicenseID", myPurchase.LicenseID), New XAttribute("PurchaseDate", myPurchase.PurchaseDate), _
New XAttribute("Income", myPurchase.Income), New XAttribute("Comments", myPurchase.Comments)))
myDocument.Add(MyCustomer)Now I want to add this Rule as a child of the Purchase XElement above
Dim Rule As XElement = New XElement("Rule", _
New XAttribute("Attribute", "SubscriptionDate"), _
New XAttribute("Value", DateTime.Now.AddDays(30).ToString("s")))What is the best way? There will be at least one rule but there could be as many as 4 total.
Todas las respuestas
-
martes, 06 de marzo de 2012 16:07
Hi Dave;
First get a reference to the Purchase element.
Dim pElement = (From p In myDocument.Descendants("Purchase")
Select p).FirstOrDefault();
Create your Rule element/s and add them to the pElementDim rule = new XElement("Rule", "Rule 1");
pElement.Add(rule);Now the rule is a child of Purchase
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Editado Fernando Soto - MCSDMicrosoft Community Contributor martes, 06 de marzo de 2012 16:36 Correct mis-name variable
- Marcado como respuesta Super Dave Osborne martes, 06 de marzo de 2012 17:55
-
martes, 06 de marzo de 2012 16:20
I not sure what the Select node command is, maybe a C# thing? Using p works...
Thanks
Dim myDocument As New XDocument
myDocument.Declaration = New XDeclaration("1.0", "utf-8", "no")
myDocument.Add(New XComment("XComment"))
myDocument.AddAnnotation("XAddAnnotation")
Dim MyCustomer As New XElement("Customer", New XAttribute("Name", MyCust.Name), New XAttribute("EMail", MyCust.Email), New XAttribute("FirstName", MyCust.FirstName), _
New XAttribute("LastName", MyCust.LastName), New XAttribute("Salutation", MyCust.Salutation), _
New XAttribute("Company", MyCust.Company), New XAttribute("Address", MyCust.Address), New XAttribute("Address2", MyCust.Address2), _
New XAttribute("City", MyCust.City), New XAttribute("StateCode", MyCust.StateCode), New XAttribute("CountryISOCode", MyCust.CountryISOCode), _
New XAttribute("PostCode", MyCust.PostCode), New XAttribute("Phone", MyCust.Phone), New XAttribute("Fax", MyCust.Fax), _
New XAttribute("Comments", MyCust.Comments), _
New XElement("Purchase", New XAttribute("LicenseID", myPurchase.LicenseID), New XAttribute("PurchaseDate", myPurchase.PurchaseDate), _
New XAttribute("Income", myPurchase.Income), New XAttribute("Comments", myPurchase.Comments)))
myDocument.Add(MyCustomer)
Dim pElement = (From p In myDocument.Descendants("Purchase")
Select p).FirstOrDefault
Dim Rule As XElement = New XElement("Rule", _
New XAttribute("Attribute", "SubscriptionDate"), _
New XAttribute("Value", DateTime.Now.AddDays(30).ToString("s")))
pElement.Add(Rule)- Marcado como respuesta Super Dave Osborne martes, 06 de marzo de 2012 16:21
- Desmarcado como respuesta Super Dave Osborne martes, 06 de marzo de 2012 17:55
-
martes, 06 de marzo de 2012 16:35
I tested the code before posting and when I posted it I renamed some variable, seems I overlooked one.
Did you really mean to mark your post ans a solution?
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". -
martes, 06 de marzo de 2012 17:32
I could change it to yours but do you know why node doesn't work? I only did mine because of what I thought was a error in that node is not recognized. That you put a ; behind the call also makes me think you did it in C# not vb. Didn't want someone else to have to figure out why that part didn't work. If you want to post a copy of mine I will switch it for your credit.
node is either not declared or not in the current scope. winforms 4.0 .net
Here are my includes.
Imports System.Net
Imports System.IO
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization
Imports System.Runtime.Serialization.Formatters
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Text.RegularExpressions
Imports System.Exception
Imports System.ServiceModel
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports WindowsApplication1

