xelement

Declare namespaces within XPath expression

江枫思渺然 提交于 2019-12-01 20:41:32
问题 My application needs to evaluate XPath expression against some XML data. Expression is provided by user at runtime. So, I cannot create XmlNamespaceManager to pass to XPathEvaluate because I don't know prefixes and namespaces at compile time. Is there any possibility to specify namespaces declaration within xpath expression? Answers to comments: XML data has one default namespace but there can be nested elements with any namespaces. User knows namespaces of the data he works with. User

using XElement to query for a node in namespace

拟墨画扇 提交于 2019-12-01 16:40:07
i'm trying to pull out a node from a csproj file that looks like this, but can't get it to work - presumably because of the namespace declaration. <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <RegisterForComInterop>true</RegisterForComInterop> This fails miserably: XDocument cpo = XDocument.Load(file); XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable()); nsm.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/200"); IEnumerable<XElement> list3 =

using XElement to query for a node in namespace

心不动则不痛 提交于 2019-12-01 15:07:40
问题 i'm trying to pull out a node from a csproj file that looks like this, but can't get it to work - presumably because of the namespace declaration. <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <RegisterForComInterop>true</RegisterForComInterop> This fails miserably: XDocument cpo = XDocument.Load(file); XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable()); nsm

Linq To Xml problems using XElement's method Elements(XName)

此生再无相见时 提交于 2019-12-01 06:47:17
I have a problem using Linq To Xml. A simple code. I have this XML: <?xml version="1.0" encoding="utf-8" ?> <data xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/directory file.xsd"> <contact> <name>aaa</name> <email>email@email.ext</email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description>Description for this contact</description> </contact> <contact> <name>sss</name> <email>email@email.ext</email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description

How to Read a specific element value from XElement in LINQ to XML

情到浓时终转凉″ 提交于 2019-12-01 05:21:51
I have an XElement which has content like this. <Response xmlns="someurl" xmlnsLi="thew3url"> <ErrorCode></ErrorCode> <Status>Success</Status> <Result> <Manufacturer> <ManufacturerID>46</ManufacturerID> <ManufacturerName>APPLE</ManufacturerName> </Manufacturer> //More Manufacturer Elements like above here </Result> </Response> How will i read the Value inside Status element ? I tried XElement stats = myXel.Descendants("Status").SingleOrDefault(); But that is returning null. XElement response = XElement.Load("file.xml"); // XElement.Parse(stringWithXmlGoesHere) XNamespace df = response.Name

How to avoid System.Xml.Linq.XElement escaping HTML content?

蓝咒 提交于 2019-11-30 21:40:09
I'm using the XElement object to build some HTML in the code-behind on an ASP.NET page. I may or may not add some XAttributes to this XElement as I go along, in the following fashion: var elmnt = new XElement("div", new XAttribute("id", "myDiv"), ); Now, if I want to add some content into myDiv which contains HTML, the XElement automatically escapes this which, in my situation, is undesirable. So if I have: var elmnt = new XElement("div", new XAttribute("id", "myDiv"), "<span id='content'>hello world</span>" ); And then I render this into a Placeholder object using the following code:

How to avoid System.Xml.Linq.XElement escaping HTML content?

℡╲_俬逩灬. 提交于 2019-11-30 05:21:31
问题 I'm using the XElement object to build some HTML in the code-behind on an ASP.NET page. I may or may not add some XAttributes to this XElement as I go along, in the following fashion: var elmnt = new XElement("div", new XAttribute("id", "myDiv"), ); Now, if I want to add some content into myDiv which contains HTML, the XElement automatically escapes this which, in my situation, is undesirable. So if I have: var elmnt = new XElement("div", new XAttribute("id", "myDiv"), "<span id='content'

Children of XElement

谁说胖子不能爱 提交于 2019-11-30 04:36:01
How do I get just the children of an XElement? I am currently using the XElement.Descendants() function, which returns all levels of XElements, rather than just the child nodes. What I would really like is an IEnumerable of just the children. The immediate child elements of one XElement are accessible by calling the Element() or Elements() functions. Use the overloads with a name to access specific elements, or without to access all child elements. There are also similar methods like Attribute() and Attributes() that you might find useful. XElement.Nodes() should get you what you want. If you

XElement => Add children nodes at run time

扶醉桌前 提交于 2019-11-29 23:41:53
So let's assume this is what i want to achieve: <root> <name>AAAA</name> <last>BBBB</last> <children> <child> <name>XXX</name> <last>TTT</last> </child> <child> <name>OOO</name> <last>PPP</last> </child> </children> </root> Not sure if using XElement is the simplest way but this is what I have so far: XElement x = new XElement("root", new XElement("name", "AAA"), new XElement("last", "BBB")); Now I have to add the "children" based on some data i have. There could be 1,2,3,4 ... so I need to iterate thru my list to get every single child foreach (Children c in family) { x.Add(new XElement(

How to best detect encoding in XML file?

≯℡__Kan透↙ 提交于 2019-11-29 10:49:10
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 = new XmlParserContext(null, xmlns, "", XmlSpace.Default, encoding); using (var reader = XmlReader.Create(filepath, settings, context)) { return XElement.Load(reader); } This works, but it seems a bit inefficient to open the file twice. Is there a better way to detect