xelement

Best way to get InnerXml of an XElement?

What's the best way to get the contents of the mixed "body" element in the code below? The element might contain either XHTML or text, but I just want its contents in string form. The XmlElement type has the InnerXml property which is exactly what I'm after. The code as written almost does what I want, but includes the surrounding <bo...

Extract a Byte[] from an XElement with Linq to Xml

Hi, I am saving some small images to Xml as a Byte[] via the following XElement construct.. XElement xe = new XElement("Images", from c in qry select new XElement("Image", new XAttribute("Date", c.Date), new XElement("Data", c.Bytes))); the Bytes property is a Byte[], looking at the resulting elem...

Creating XML with namespaces and schemas from an XElement

A longwinded question - please bear with me! I want to programatically create an XML document with namespaces and schemas. Something like <myroot xmlns="http://www.someurl.com/ns/myroot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.someurl.com/ns/myroot http://www.someurl.com/xml/...

Want to return the entire contents of an XElement as a string

I'm a newbie to LINQ in C# and am using it to read in and work with XML files. I'm able to navigate up and down my elements, but what I want for some elements is to return the entire contents as a string. Meaning, I have an element like this: <element1> <subel1> some text here </subel1> </element1> When I get the value of elemen...

How to write out the contents of a string to XML without the codes...?

I have a string that has angle brackets in it like this: <element1>my text here</element1> The string literally looks like this when I write it to console or my dataGridView or anywhere else. However, I'm trying to write this as part of an XML document. Everything is fine except that in the xml file that is written, the above show...

Returning an XElement from a web service

Is it possible to return an XElement from a webservice (in C#/asp.net)? Try a simple web service that returns an XElement: [WebMethod] public XElement DoItXElement() { XElement xe = new XElement("hello", new XElement("message", "Hello World") ); return xe; } This compiles fine but if you try and run it you get Cannot...

Get the XPath to an XElement?

I've got an XElement deep within a document. Given the XElement (and XDocument?), is there an extension method to get its full (i.e. absolute, e.g. /root/item/element/child) XPath? E.g. myXElement.GetXPath()? EDIT: Okay, looks like I overlooked something very important. Whoops! The index of the element needs to be taken into account. S...

Need a little help with Linq 2 xml

Hi! I have a similar scenario as this one: public class TestLinq2Xml { private XElement GenerateSomeXml() { return XElement.Parse(@"<MyObject> <Properties> <Name>My object 1</Name> <Position>0; 0; 0</Position> ...

removing xmlns in XElement

Hello Folks, I am parsing a XML string in XElement.Parse("somestring") and insert it into another XElement using add method. so, i want to remove the default utf encoding and xmlns attributs from "somestring" text. How to do that... I appreciate your help. Thanks KJ ...

Delete XElement based on attribute

Hi, I'm still playing around with xml. Now I have a file that looks like this: <?xml version="1.0" encoding="utf-8"?> <Attributes> <AttributeSet id="10110"> <Attribute id="1">some text here</Attribute> <Attribute id="2">some text here</Attribute> <!-- 298 more Attribute nodes follow --> <!-- note that the value for the id att...

How to best detect encoding in XML file?

To load XML files with arbitrary encoding I have the following code: Encoding encoding; using (var reader = new XmlTextReader(filepath)) { reader.MoveToContent(); encoding = reader.Encoding; } var settings = new XmlReaderSettings { NameTable = new NameTable() }; var xmlns = new XmlNamespaceManager(settings.NameTable); var context =...

XML serialize annotations

I have a situation where I have an xml file that I don't want to modify. The AddAnnotation function in XElement class provides an option to add memory-only data which is not serialized and not part of the XML. I want to be able to save these annotations (for example: to another xml file) and then to deserialize both the xml and the ann...

How to apply a DataTemplate to a XElement subclass?

Hi, when I subclass from XElement, the DataTemplate that works for XElement using the element name as DatatType, doesn't work for the subclass. Any idea? <HierarchicalDataTemplate DataType="grupo" ItemsSource="{Binding Path=Elements}"> <TextBlock Text="{Binding Path=Attribute[name]}" /> </HierarchicalDataTemplate> <!-- When I bu...

Loading a IEnumerable<XElement> collection

XElement root = XElement.Load(xmlReader); IEnumerable<XElment> items = root.Elements("?????????"); Where the ???? is, can I add a path or it has to be a single xml element name? ie. can I do /blah/blah2/asdf ? ...

Auto-binding an "XElement" property in ASP.NET MVC

I have an "Edit" action and an "Edit" view to allow users to update a certain entity in the database. It's database type is "XML", and the DataContext (I'm using Linq-to-SQL) represents it as a property of type "XElement". In my view, I render a text-area from the "ToString()" output of the propery like this: <%= Html.TextArea("Text",...

How do I work with an XML tag within a string?

I'm working in Microsoft Visual C# 2008 Express. Let's say I have a string and the contents of the string is: "This is my <myTag myTagAttrib="colorize">awesome</myTag> string." I'm telling myself that I want to do something to the word "awesome" - possibly call a function that does something called "colorize". What is the best way ...

Shared XElement as SiteMap

I have a custom CMS in which I use an static XElement as the site map. When updates occur to the map, I am synchronizing the writer threads, but am doing nothing with the readers, just allowing them to grab the XElement when they need it. In testing, I thought that if I was enumerating the XElement from a reader thread, while I update...

What is an appropriate XML type for web service?

Current Implementation Sql Server 2005 Database with a table called messages with a column called MessageXml of type xml. C# Library project with a Linq to Sql class that generates a class called Message with a field called MessageXml of type XElement. WCF Webservice that exposes a MessagePayload class with a property called MessageXm...

XElement: a collection of all the leaves?

Hi, how to get a collection of all the leaves of a XElement tree regardless the hierarchy? Thanks ...

XElement and List<g>

I have a class that has the following properties: public class Author { public string FirstName { get; set; } public string LastName { get; set; } } Next, I have a List of Authors like so: List<Author> authors = new List<Author> (); authors.add( new Author { FirstName = "Steven", LastName = "King" }); authors.add( ...