linq-to-xml

Why XDocument can't get element out of this wellform XML text?

蓝咒 提交于 2019-12-31 02:49:06
问题 I'm trying to get the value of the Address element from the following XML text, but it's not finding it unless I remove xmlns="http://www.foo.com" from the Root element. However, the XML is valid even with it. What's the problem here? Since I'm getting the XML text from a web service, I don't have control over it, but I can strip out the xmlns part if I have to as the last resort. <?xml version="1.0" encoding="utf-8"?> <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=

Get max attribute value from XML using LINQ

怎甘沉沦 提交于 2019-12-30 18:57:05
问题 I have the following XML file. I want to get Max("NR") using LINQ . Could anyone help me to do this? I know how to do this for nodes, but attributes made me confused... :S <?xml version="1.0" encoding="utf-8"?> <SMPyramid LayerName="qwe" LayerPath="D:\#PYRAMID\qwe" Extension=".png" Meters="100000" RasterSize="4000"> <Level NR="0" RasterXSize="512" RasterYSize="512" LastTileXSize="416" LastTileYSize="416" MinX="400000" MaxX="500000" MinY="1200000" MaxY="1300000" ScaleFactor="25" TilesCountX="8

Get max attribute value from XML using LINQ

落爺英雄遲暮 提交于 2019-12-30 18:56:09
问题 I have the following XML file. I want to get Max("NR") using LINQ . Could anyone help me to do this? I know how to do this for nodes, but attributes made me confused... :S <?xml version="1.0" encoding="utf-8"?> <SMPyramid LayerName="qwe" LayerPath="D:\#PYRAMID\qwe" Extension=".png" Meters="100000" RasterSize="4000"> <Level NR="0" RasterXSize="512" RasterYSize="512" LastTileXSize="416" LastTileYSize="416" MinX="400000" MaxX="500000" MinY="1200000" MaxY="1300000" ScaleFactor="25" TilesCountX="8

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

喜夏-厌秋 提交于 2019-12-30 09:22: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. 回答1: XElement

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

拟墨画扇 提交于 2019-12-30 09:22:07
问题 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. 回答1: XElement

XPath and XPathSelectElement

醉酒当歌 提交于 2019-12-30 02:48:11
问题 I have the following xml <root> <databases> <db1 name="Name1" /> <db2 name="Name2" server="myserver" /> <db3 name="Name3" /> </databases> <root> I've tried everything to read the name of the db2 (="Name2") with all possible combinations of XPath queries, but never get the expected result. My Code so far: var query = "root/databases/db2.. "; // here I've tried everything var doc = XDocument.Load("myconfig.xml"); var dbName = doc.XPathSelectElement(query); What's the correct query to get my

Validating XML with XSD

末鹿安然 提交于 2019-12-30 02:19:12
问题 I'm running into real difficulties validating XML with XSD. I should prefix all of this and state up front, I'm new to XSD and validation, so I'm not sure if it's a code issue or an XML issue. I've been to XML API hell and back with the bajillion different options and think that I've found what would be the ideal strategy for validating XML with XSD. Note, my XML and XSD are coming from a database, so I don't need to read anything from disk. I've narrowed my problem down into a simple sample

How do you guard for Null Reference exceptions in Linq To Xml?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 06:44:44
问题 <?xml version="1.0" encoding="utf-8" ?> <pages> <page id="56"> <img id="teaser" src="img/teaser_company.png"></img> </page> </pages> I have an xml file that defines additional resources for pages within a cms. What's the best way to guard for Null Reference exceptions when querying this file with LinqToXml? var page = (from tabElement in extensionsDoc.Descendants("page") where tabElement.Attribute("id").Value == tabId.ToString() select tabElement).SingleOrDefault(); This code could

What is the fastest way to load XML into an XDocument?

*爱你&永不变心* 提交于 2019-12-28 19:38:34
问题 When you create a new XDocument using XDocument.Load , does it open the XML file and keep a local copy, or does it continuously read the document from the hard drive? If it does continuously read, is there a faster way to parse XML? XDocument x = XDocument.Load("file.xml"); 回答1: There are a couple of measurements to consider: Linear traversal speed (e.g. reading/loading) On-demand query speed To answer the immediate question: XDocument uses an XmlReader to load the document into memory by

LINQ to XML in VB.NET

筅森魡賤 提交于 2019-12-28 15:37:31
问题 I'm basically brand new to LINQ. I've looked around a lot on here and am pretty confused. I've seen some examples that allow me to strong type objects using LINQ but I don't really understand them because they're in C#, which I guess lets you do different things with LINQ(I think?). Anyways, this is what I'm trying to do: Dim productXML As XDocument = XDocument.Load( _ Server.MapPath("~/App_Data/products.xml")) Dim products As List(Of Product) = 'some query to select all products ?' 'set up