linq-to-xml

Convert XDocument to Stream

大城市里の小女人 提交于 2019-12-28 05:35:15
问题 How do I convert the XML in an XDocument to a MemoryStream, without saving anything to disk? 回答1: Have a look at the XDocument.WriteTo method; e.g.: using (MemoryStream ms = new MemoryStream()) { XmlWriterSettings xws = new XmlWriterSettings(); xws.OmitXmlDeclaration = true; xws.Indent = true; using (XmlWriter xw = XmlWriter.Create(ms, xws)) { XDocument doc = new XDocument( new XElement("Child", new XElement("GrandChild", "some content") ) ); doc.WriteTo(xw); } } 回答2: In .NET 4 and later, you

LINQ multiple columns

£可爱£侵袭症+ 提交于 2019-12-27 02:07:08
问题 <root> <data1> <Element1>Value</Element1> <Element2>Value</Element2> <Element3>Value</Element3> </data1> <data2> <Element1>Value</Element1> <Element2>Value</Element2> </data2> </root> From the above XML I would like to make an XML looking like this: <root> <d1e1>value<d1e1> <d1e2>value<d1e2> <d2e1>value<d2e1> </root> What is the most efficient way to process that? Foreach or Linq in theory Linq should be faster in most cases and speed is of the essence for this project Any idea? 回答1: The idea

LINQ multiple columns

十年热恋 提交于 2019-12-27 02:07:05
问题 <root> <data1> <Element1>Value</Element1> <Element2>Value</Element2> <Element3>Value</Element3> </data1> <data2> <Element1>Value</Element1> <Element2>Value</Element2> </data2> </root> From the above XML I would like to make an XML looking like this: <root> <d1e1>value<d1e1> <d1e2>value<d1e2> <d2e1>value<d2e1> </root> What is the most efficient way to process that? Foreach or Linq in theory Linq should be faster in most cases and speed is of the essence for this project Any idea? 回答1: The idea

Return all Elements and Sub-elements

梦想的初衷 提交于 2019-12-25 18:58:18
问题 Is it possible to use one LINQ query to return the values of all elements and child elements at one time? Using the query below I'm able to retrieve the first element, but not the child elements. var query = from c in xDoc.Descendants("file") orderby c.Name select new { // This gets the main elements Name = (string)c.Element("name").Value, }; The XML File looks like this: <files> <file id="1"> <name>A file</name> <processDetails> <purpose>It's supposed to get files.</purpose> <filestoProcess>

parse the xml by specifying the attribute names

喜你入骨 提交于 2019-12-25 18:14:56
问题 I have a xml from which I want to parse only specific attributes not all. I have 100s of attributes and the xml I provided is a sample with few attributes . I want explicitly specify the attributes names and parse their values. Eg : I want to parse get the values of the Attribute names PersonN , VerifiedHuman In my logic I want to parse the values by specifying attribute names like <Name>PersonN</Name> and parse its value The result should be a csv. <InterConnectResponse> <SchemaVersion>2.0<

Parsing XML with VB.NET with LINQ To XML

旧巷老猫 提交于 2019-12-25 16:46:21
问题 I have an incoming XML stream from a web service response that looks like so <?xml version="1.0" encoding="utf-16"?> <HotelPropertyDescriptionRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" TimeStamp="2013-12-30T18:49:36" Version="1.14.1"> <Success xmlns="http://webservices.sabre.com/sabreXML/2003/07" /> <RoomStay xmlns="http://webservices.sabre.com/sabreXML/2003/07"> <RoomRates> <RoomRate GuaranteeSurchargeRequired="G"

Linq to XML in .net 2.0

回眸只為那壹抹淺笑 提交于 2019-12-25 13:18:09
问题 IS there a way to use Linq to XML in .net 2.0? I'm not sure about it, if not, how could i recode this var doc = XDocument.Load("config.xml"); var xVideo = doc .Element("XML") .Element("VIDEO"); xVideo.SetElementValue("MAPTEXTURELEVEL", 8); doc.Save("config.xml"); Without using Linq to XML? 回答1: XmlDocument doc = new XmlDocument(); doc.Load("config.xml"); XmlNode node = doc.SelectSingleNode("/XML/VIDEO/MAPTEXTURELEVEL[1]"); node.InnerText = "8"; doc.Save("config.xml"); No, you can't use

XDocument.Parse: Avoid replacing XXE references

本小妞迷上赌 提交于 2019-12-25 12:12:46
问题 I'm trying to protect against malicious XXE injections in the XMLs processed by my app. Therefore I'm using XDocument instead of XmlDocument. The XML represents the payload of a web request so I call XDocument.Parse on its string content. However, I'm seeing the XXE references contained in the XML (&XXE) being replaced in the result with the actual value of ENTITY xxe. Is it possible to parse the XML with XDocument without replacing &xxe ? Thanks EDIT: I managed to avoid the replacement of

Can't Retrieve Values of XML Elements using XElement

送分小仙女□ 提交于 2019-12-25 07:35:08
问题 I am trying to read from an XML file that is supplied through a file browser but the values are alwas null even though I can see the xml file come through to elements. public void UploadXml(Stream fileStream) { //Load xml fileStream.Position = 0; var xdoc = XElement.Load(fileStream); IEnumerable<XElement> elements = xdoc.Elements(); var codeList = new CodeList(); foreach (var item in elements) { codeList.Name = item.Element("CODELIST_NAME").Value; codeList.Description = item.Element(

How can i parse html file in windows phone 7?

点点圈 提交于 2019-12-25 07:22:00
问题 Hi am using xml file given below,i want to parse html file . <Description> <Fullcontent> <div id="container" class="cf"> <link rel="stylesheet" href="http://dev2.mercuryminds.com/imageslider/css/demo.css" type="text/css" media="screen" /> <ul class="slides"> <li>Sonam Kapoor<img src="http://deys.jpeg"/></li> <li>Amithab<img src="http://deysAmithab.jpeg"/></li> <li>sridevi<img src="http://deyssridevi.jpeg"/></li> <li>anil-kapoor<img src="http://deysanil-kapoor.jpeg"/></li> </ul> </div> <