Adding XElement As A Child Of An Existing XElement Linq?

Mar 6, 2012

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"))

[Code]....

What is the best way? There will be at least one rule but there could be as many as 4 total.

View 4 Replies


ADVERTISEMENT

Creating A Child Class Of XElement That Still Keeps A Reference To Original XElement And Preserves Tree Structure

Dec 20, 2010

I'm working with a class that inherits the XElement class.The new class is called MXElement.It adds some new functionality to it for navigating through the XML tree, as well as some more information regarding attributes, but that's not particularly important.My problem is that I have an XML Tree filled with XElement objects.However, when I create a new MXElement object from the XElement object before, it is just a copy of that object. This means that any changes that I make to this object will not effect the original tree.I suppose what I'm asking for is a way to build in the functionality for my MXElement class and keep references to the original XElement objects.If possible, I would really prefer to keep MXElement a child class instead of building a lot of extension methods for XElement.

View 1 Replies

XElement.Add(XElement) Automatically Adds Namespace To Child Node?

Nov 4, 2011

I am using XElement to manipulate my xml file: to find target node and then add child node to it. But now I have one problem. Let's say my xml file looks like this:

<Report xmlns="MY_NAMESPACE"
<Width>100</Width>
<Height>100</Height>

[Code].....

The second <ReportItem> is the newly-added one, but sadly this is not a valid file (my parser is complaining in the further processing of xml file). So how can I make this new item look exactly like the existing one, without any namespace? This has been driving me crazy. I spent a whole day to figure out that I need to add the curly brackets so that it will find the target node (it doesn't work like what the link above says that it adds and searches the default NS automatically), but now it adds something unnecessary?

View 1 Replies

Adding Xelement As A Sibling Of Xelement Without A Parent?

Oct 11, 2010

I'm trying figure out if this is possible. Basically, I need to create an XElement and then add one or more siblings to that XElement, but without a parent. I'll add that list of XElements to a Parent later, but need some flexibility in building this and other lists of XElements before doing so. Is this possible?

So I would have something like:

Public items As XElement = <ItemA>Something</ItemA>

And then I need to add an element so that the result looks like:

<ItemA>Something</ItemA>
<ItemB>Something Else</ItemB>

That result is what I need to pass around as a single object. I've messed arounnd with IEnumerable(Of XElement), but there is no .Add.

View 1 Replies

Conditional XElement Content / XElement Or XAttribute

Feb 15, 2011

I'm building XML based on some data pulled from a database, and thought to try building the XML with LINQ2XML. The data is coming from a few different places, so I've been building the XML by sections, based on what I have populated in DataTable objects. Using the XElements and XAttributes, I can get one or the other, but I was wondering if anyone had insight in how to make the determination of whether to use XElement or XAttribute at the point where the XML is being built.[code]So it's in that LINQ query that I need to determine if ValueToParse, for example, has a valid value (non-empty). If it does, then I can add the node and it's value. If it DOES NOT, I need to add that named node, but with an attribute of 'xsi:nil="true"',The caveats are that that there are some descendants which repeat over different parents (for example, there's an AddressDetails section which contains AddressDetail elements. These AddressDetail elements can contain 0 or 1 Contacts groups of Contact elements, so adding a node to the Contacts node isn't a straightforward option as it would be if there was only ONE Contacts element).I don't usually USE LINQ, but it SEEMED like a good idea at the time XD

View 1 Replies

Adding Namespace Attribute To XElement - Prevent Blank/empty Namespace On Child Elements?

Mar 17, 2011

I need to read an xml document from a database record into an XDocument object in order for it to be deserialized. So that the deserialization will work, I need to apply a specific namespace to each of the level 1 elements. So XML looks a bit like this:

[Code]...

How do I prevent the blank/empty namespace being added to each child element of the element to which the required namespace has been applied?

View 1 Replies

.net - Convert Linq.Xelement To Stream For XMLTextReader?

Nov 16, 2010

I am producing an XML file in my unit test using

Public Sub rssParserTest
Dim Const rssUri as String = "rssTestFile.xml"
Dim xmlFile = <rss version="2.0">

[Code]...

I want to remove the unit test dependency on a physical file and use a stream instead but my efforts so far have come to nought. (Is this best practise?)I am using NMock2 for mocking if I should be doing something with that.

View 2 Replies

Loading A Xml Document From A File Built Use Linq.Xelement

Jan 11, 2012

I'm trying to load in a xml file that was build using another windows form, but when I try the xml document I get the following error: Name cannot begin with the '%' character, hexadecimal value 0x25. Line 10, position 13.[code]

View 3 Replies

Error 1 Operator '&' Is Not Defined For Types 'String' And 'System.Xml.Linq.XElement'

May 17, 2012

i am downloading data from xml using linq library those data i want to add them on a textblock item

TextBlock1.Text = TextBlock1.Text & result

but it has an error :

Error 1 Operator '&' is not defined for types 'String' and 'System.Xml.Linq.XElement'.

When i am changing this line to :

TextBlock1.Text = TextBlock1.Text & result.Tostring

it works but it adds this data :

"<"data> data <"/data>

instead of :

hello

View 1 Replies

Cannot Convert System.Xml.XmlNode To System.Xml.Linq.XElement

Nov 24, 2010

I´m gettin the error Calling the fuction GetListItems but is kind weird because it works in Visual Studio 2008 Express but no in Visual Basic 2010 Express:

Dim ndQuery As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "")
Dim ndViewFields As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "")
Dim ndQueryOptions As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element,

[Code].....

View 1 Replies

C# - Get InnerText From XElement?

Mar 19, 2011

I have an XML file I need to parse, but only for the text, not the HTML. Here's an example of a node:

<highlights><![CDATA[<ul style="color:#000000;font-size:small;font-family:verdana,geneva,sans-serif"><li>Classy &amp; elegant purse hooks</li><li>Choose from various styles<br></li><li>Stable and reliable</li><li>Makes a great gift! </li></ul> ]]></highlights>

As you can see, the CDATA has HTML code in it, so when I do element.Value I also get the tags. Is there any simple way to get just the text?

View 2 Replies

How To Set An Empty XElement

Dec 22, 2009

Im trying to add a Xelement to an XElement. But when I tried to add this XElement to the empty XElement VB throws "Object reference not set to an instance of an object" The Bold line code throws the exception

here the sample code: Private Sub Button_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_OK.Click

[Code]...

View 3 Replies

XElement And An XPath Expression ?

Feb 1, 2010

I have an XElement and an XPath expression that selects a node from it. It works fine in the original program that I wrote it in but doesnt work if I paste it to another program. I've checked the references and import statements.

I think the problem might be that the original program had the code within the class of a wpf window while it was moved to a abstract class where it doesnt work.[code...]

View 1 Replies

Get Only Direct Descendants Of An XElement?

Aug 4, 2009

[code]...

In this case, children includes all the Parent elements and all of the Child elements. What's the best way to grab only the direct descendants of <Root>?

Should I write a LINQ query that selects elements where parent = <Root>? Or is there some built-in method I'm missing that can get this for me?

EDIT: I had some confusion between XElement.Elements and XElement.Descendants. As Ruben Bartelink pointed out, XElement.Elements will give me exactly what I was looking for.

View 4 Replies

Test To See When An Xelement Exists?

Mar 6, 2009

I'm reading an XML file that looks like this[cod]...

The resulting List(Of Vehicle) is then bound to an ASP.NET ListView control where the data is rendered. All goes well if all the desired nodes are present. For example, the Mileage node is conditional and is absent sometimes. Whenever the node isn't present I get a "Sequence contains no elements" error.

I've tried everything I know to get things working properly. Is there a way to test if an XElement exists?

View 1 Replies

Typecasting XElement To String?

Jun 11, 2009

I am trying to retrieve a value from an xml file via the XElement and pass it on to a function for further logic testing / et al but keep getting an eror: Here is the code I am using

dim path as string = "C:UsersPaulmyfile.xml"
dim doc as xelement =xelement.load(path)

'This is the line that i would like to convert i.e the sdr to string ? dim sdr = from d in doc.descendants("BOOKS") Select d.Value

when I pass the value sdr, I get an error 13 "Conversion from type "WhereSelectEnumerableIterator(Of XElement, String) to type string is not valid. I have also tried to pass it as an object but can not seem to be able to typecast it to string.

View 15 Replies

Use For Each Loop With A Where Clause For XElement?

Oct 5, 2010

I am tring to write a for each loop that loops through the descendants of an xml doc but only the ones that satisfy a criteria.[code]...

View 1 Replies

XElement And Namespaces [VB 2010]?

Dec 2, 2011

I have two XML files.

Consumers.xml
<consumers>
<consumer>consumer1</consumer>

[code].....

View 2 Replies

Xml - Reading Childelement To XElement In .NET?

Dec 22, 2011

I've been trying to read an XML-file using VB.NET and after some (see 2-3 hours) googling and reading I've headed in somewhat the right direction, however now I'm at a stop and I can't find any answers that makes any sense to me.

[Code]...

View 1 Replies

IEnumerable<XElement>.Value() Extension Method Available In .Net But Not C#?

Jun 5, 2012

In VB.Net you can easily get the text value of the first child element of an XElement like so:Dim sChildValue = xeParent.<MyChild>.Value()Of course that syntax is not supported in C#, but it produces the same IEnumerable(Of XElement) result as the Elements() method. So we can rewrite the above like so:

Dim sChildValue = xeParent.Elements("MyChild").Value()This Value() extension method is handy because often you are working with small XML documents and you just want the first node that matches. (One thing that annoys me about this method is that it seems to return Nothing/null if the IEnumerable list is empty due to the specified element not being found. To work around this I have created my own ValueOrBlank() method that returns an empty string in that situation instead.)

My problem is that I can't do the same thing in C#:var sChildValue = xeParent.Elements("MyChild").Value(); // won't compile, can't find Value() method

I checked my references/imports and they match the VB.Net project where the same call works. Fortunately I am able to use my custom ValueOrBlank() extension method to accomplish the same thing. But I'm curious as to what's missing in my C# project. I tried right-clicking on the Value() method call in Visual Studio and clicking "Go To Definition" to see if the Object Browser could tell me where the extension method is kept, but it just shows the System.String class. Does that mean this is one of those sneaky VB.Net-only features that the compiler itself supports, like the XML Axis < MyChild > syntax?

View 2 Replies

Read Xml Files Which Contain Namespaces With Xelement?

Jun 9, 2011

I have the file xmlcontents.xml with the following content[code]....

View 4 Replies

VS 2008 Read These Values From XML With XElement?

May 2, 2012

im having problems with reading these values from XML with XElement Im trying to get Each value between <month> and </month>

<Document>
<Element>
<status>1</status>
<data>

[code]....

View 2 Replies

XElement.Element.value Throws Null Reference Exception

Aug 10, 2011

Goal: Load a validated xml file in to memory, traverse through the document to create objects and load object properties with values in named xml tags.Problem: How do I deal with missing tags?Amplifying information: in this example, there are three values I'm looking for; name (mandatory), location (optional), comments(optional). I found the problem in prototyping the solution. My inital test was with an xml file wtih all fields present. It works fine as long as I don't have nanespace declarations in the root element. However, if I leave them in place I get a null reference exceptoin. The same thing occurs if I leave off an optional tag. It appears that if the Element I'm trying to get a value on doesn't exist within that element, a null reference exception is thrown.

Code:

Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
Dim xelement As XElement = xelement.Load(txtXMLfileLoc.Text)
Dim ICAOxs As IEnumerable(Of XElement) = xelement.Elements()

[code]....

View 5 Replies

XElement.Parse Unexpected End Of File. Elements Are Not Closed

Jun 10, 2011

I am trying to parse incoming data from a serial port that appears to be XML format but an exception is thrown when I switch from a simulated setup to the real deal.My program runs great in a simulated setup but when I try it out on the real device I get: XmlException was unhandled Unexpected end of file has occurred. The following elements are not closed: li840 line 1, position 9.The li840 tag is my root tag and the position never seems to get past 40. I've tried using a try catch block but in my in both my simulated and real setups my values are blank but the program will run without exceptions. Should I take my received data and put it into a file and then read from the file? or is there something else I can do to make this work properly? [code]

View 12 Replies

XML Literals Parsing - XElement.Parse(variable Containing Text)

Sep 4, 2009

If I have a text stored in db, file, resource does not matter:

<code><%= Integer.MaxValue %></code>

Is there a way to do this: Dim el As XElement = XElement.Parse({variable containing text above}) and to have it evaluate the expression Integer.MaxValue?

View 3 Replies

Storing SQL Strings In XML XElement Instead Of String To Preserve Line Breaks?

Jun 22, 2010

When it comes to storing SQL in code, most people use strings:Dim strSql As String = "Select Foo, Bar From FooBarTable Where Something = @Something"The problem is, when you have very long SQL strings, reading them in code becomes difficult. You can usually split the strings up like this:

Dim strSql As String = "Select Foo, Bar From FooBarTable " & _
"Where Something = @Something"
This causes a number of problems:

[code].....

View 2 Replies

Add Child Elements To Existing Xml - Linq?

Aug 17, 2009

I'm having trouble adding child elements to the below
<securities>
<security id="040104JC6">

[code]....

View 2 Replies

XElement.Remove (XNode.Remove) Doesn't Work?

Oct 10, 2011

I'm trying to remove individual nodes from their parent, I tried the Remove method but it doesn't seem to be working. How is this done? Is this a bug or what?

Sub Main()
Dim xml =
<?xml version="1.0" encoding="utf-8"?>

[Code].....

View 1 Replies

Filter Parent / Child Table In Linq Query Based On Entities In Child Table?

Jul 9, 2010

I have a table (Projects) which is linked to projectVersions on projectID..projectVersions contains several columns on which I'd like to filter a returned project (and associated projectVersions) list. For example there is a "capacity" column and a "country" column. I am doing a filtered list of projects on one page and I'd like to include all projects where any one of the associated projectVersions has a capacity of 750ml and a country of "France" for example.It may be that a particular parameter is not set and so I pass a zero to indicate not to filter on that.I guess this needs some kind of subquery as when I try and do something like this: [code] it doesn't work as the "xxx" bit, being one-to-many, expects me to specify an item to get at the entities within and yet I want to query where ANY of the associated versions match one of the criteria.

View 1 Replies

Change Existing Form To An MDI Child?

Aug 24, 2011

I have a form already created, but now want to expand the application.I created a MDIParent, but cant seem to find how to change the existing form to an MDI child.

View 3 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved