xelement

Given an XElement, how do I retrieve a reference to another relative XElement/Xattribute given an XPath?

Given the following XML: <SomeXML> <Element1> <Element2 Attribute3="Value4" /> </Element1 </SomeXML> ... and an XElement reference to 'SomeElement' and an XPath 'Element1/Element2/@Attribute3' How do I retrieve a reference to Attribute3 so that I may alter it's value (using the Xpath)? The XPath is to be a retrieved ...

How to read from an XmlReader without moving it forwards

hey guys, I've got this scenario: while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == itemElementName) { XElement item = null; try { item = XElement.ReadFrom(reader) as XElement; } catch (XmlException ex) { //log line number a...

Problem with XElement and XslCompiledTransform

I'm having some trouble using a combination of XElement and XslCompiledTransform. I've put the sample code I'm using below. If I get my input XML using the GetXmlDocumentXml() method, it works fine. If I use the GetXElementXml() method instead, I get an InvalidOperationException when calling the Transform method of XslComiledTransform: ...

How to remove namespace from an XML Element using C#

I have an XML where I have a name space '_spreadSheetNameSapce'. In my code I have to add a new element with attribute associated with the name the space and I am doing it like the following XElement customHeading = new XElement("Row", new XAttribute(_spreadSheetNameSapce + "AutoFitHeight", "0")); It creates the ...

XElement.Load("~/App_Data/file.xml") Could not find a part of the path

hi everybody, I am new in LINQtoXML. I want to use XElement.Load("") Method. but the compiler can't find my file. can you help me to write correct path for my XML file? Note that: I defined a Class in App_Code and I want to use the XML file data in one of methods and my XML file Located in App_Data. settings = XElement.Load("App_Data/Ap...

Converting the children of XElement to string.

I am using an XElement to hold a block of HTML serverside. I would like to convert the children of that XElement into a string, sort of like an "InnerHtml" property does in javascript. Can someone help me on this please? :) ...

Converting XElement to HTML string

I am performing XML Operations on an XHTML document utilizing Xml and Linq. When converting it back to a String for outputting to a browser, it renders script tags in their original provided form when creating the XElement as <script />. That prevents it being processed correctly by most browsers on the market. I would like to perform...

StyleCop happy creation of Xml using XDocument / XElement / XAttribute

I like to create xml using the following formatting: XDocument xml = new XDocument( new XElement("Root", new XElement("A", new XAttribute("X", xValue), new XAttribute("Y", yValue)), new XElement("B", new XAttribute("Z", zValue)), new XElement("C"))); It seems easy to read and kinda flows...

writing xmlwriter contents to a linq to sql xelement field

I have a field in a table defined as xml so in my dbml it's showing up as an xelement. I have an xmlwriter in my class that needs to write it's xml to this field but calling tostring on the writer doesn't work. How do I assign the xmlwriter or it's contents to an xelement? Thanks. ...

XElement value in C#

How to get a value of XElement without getting child elements? An example: <?xml version="1.0" ?> <someNode> someValue <child>1</child> <child>2</child> </someNode> If i use XElement.Value for <someNode> I get "somevalue<child>1</child><child>2<child>" string but I want to get only "somevalue" without "<child>1</child><ch...

XElement.Value not reading multi-line data

I current have the following XML fragment that I'm parsing to read its values and display them on a web page: <root> <PostcardTitle>The Needles</PostcardTitle> <PostcardDescr>Granite Dells, Prescott</PostcardDescr> <PostcardImage> <img alt="GraniteDells" src="GraniteDells.jpg" /> </PostcardImage> <PostcardDate /> <Postca...

C Sharp serialise XML with HTML entities

I have a string in the database which contains angle brackets (less-than/greater-than). I need to insert the string into an XElement and then serialise it to a client device. Now the html entities obviously get encoded so the brackets appear as < and > and the best way may be is to use the HttpUtility.HtmlDecode to decode the entities ba...

Why doesn't XElement have a GetAttributeValue method?

Sometimes I'd like to know the reasoning of certain API changes. Since google hasn't helped me with this question, maybe stackoverflow can. Why did Microsoft choose to remove the GetAttribute helper method on xml elements? In the System.Xml world there was XmlElement.GetAttribute("x") like getAttribute in MSXML before it, both of which r...

Elegant Solution to Parsing XElements in a Namespace

In a project I am working on, I just finished writing an XSD for my XML so other engineers could work on the XML easier. Along with this XSD came XML namespaces. In my current parsing code, I have been using something like this: XElement element = xmlRoot.Element(XML.ELEMENT_NAME); With XML being a class full of constants that is used...

xElement.Value is reset when I save xlsx File

I have the following block of code which populates data from a database into an xlsx file: For Each xc As XElement In xelC Dim sAttribute_Cell As XAttribute = xc.Attribute("s") Try xelV = xc.Element(xvName) Catch ex As Exception ...

Get child elements from XElement

Hi, I want to get child elements from XElement using C#. Thanks, ...

XLinq: Remove certain XElements from a xml file which are saved in a LIst<XElement>

Hello, I can not remove nodes while I iterate them thats ok.´ I have a List with Guid`s in it. I want to delete all XElements in that xml file where the XElement has a Guid of that List thats my xml file: <?xml version="1.0" encoding="utf-8"?> <Departments> <Department Id="2d55ba71-a2ab-44a1-a697-f57bbd238c7f" /> <Department Id=...

Is there a way to create an immutable (read-only) XDocument?

I have an API that returns XElement-s, and I want the document behind those XElement-s to be immutable (read-only). I need it for: 1) Not to give devs an ability to change it accidentally :) 2) Improving performance - creating a copy of an XDocument might be a performance "heavy" operation in some cases. It doesn't seem to possible to ...

Convert IEnumerable<XElement> to XElement

The return type of my query is IEnumerable<XElement>. how can i convert the resultant data to XElement type? is it possible? could some body help me to understand this. var resQ = from e in docElmnt.Descendants(xmlns + Constants.T_ROOT) .Where(x => x.Attribute(Constants.T_ID).Value == "testid") s...

XElement.Elements(XName name) doesn't return a name that exists in XElement.Elements()

As you can see, the schema.Elements returns three elements one of whom is an EntityContainer element. But when I try to search thru the Elements overload specifying the "EntityContainer" (schema.<EntityContainer>) it doesn't retrieve anything. ...