xelement

XElement & UTF-8 Issue

£可爱£侵袭症+ 提交于 2020-01-04 17:46:08
问题 I have a .NET Web Service(.asmx, not .svc) that accepts a string via HTTP POST. The strings it accepts are xml infosets I then parse via XElement.Parse. Once parsed into an XElement instance, I add a node to one of the elements within the instance. The problem I'm having is that if a string representing an xml infoset comes through with then for some reason, me adding a node to the element XElement throws an exception such as "' ', hexadecimal value 0x06, is an invalid character. Line 1,

XElement & UTF-8 Issue

萝らか妹 提交于 2020-01-04 17:46:08
问题 I have a .NET Web Service(.asmx, not .svc) that accepts a string via HTTP POST. The strings it accepts are xml infosets I then parse via XElement.Parse. Once parsed into an XElement instance, I add a node to one of the elements within the instance. The problem I'm having is that if a string representing an xml infoset comes through with then for some reason, me adding a node to the element XElement throws an exception such as "' ', hexadecimal value 0x06, is an invalid character. Line 1,

XML serialize annotations

拟墨画扇 提交于 2020-01-04 07:25:07
问题 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 annotations in order to get the same object I had. I don't want to change the original xml and that's the reason that I use annotations. To summarize, I want to be

Remove attributes from XElement

风格不统一 提交于 2020-01-03 23:16:15
问题 I am trying to remove some attributes from xml document. Here is what I tried: private void RemoveEmptyNamespace(XElement element) { foreach (XElement el in element.Elements()) { if (el.Attribute("xmlns") != null && el.Attribute("xmlns").Value == string.Empty) el.Attribute("xmlns").Remove(); if (el.HasElements) RemoveEmptyNamespace(el); } } But it doesn't work. When I debug inside the method, attribute is removed, but when the method is fully executed, no changes were saved. The document is

Remove attributes from XElement

家住魔仙堡 提交于 2020-01-03 23:12:32
问题 I am trying to remove some attributes from xml document. Here is what I tried: private void RemoveEmptyNamespace(XElement element) { foreach (XElement el in element.Elements()) { if (el.Attribute("xmlns") != null && el.Attribute("xmlns").Value == string.Empty) el.Attribute("xmlns").Remove(); if (el.HasElements) RemoveEmptyNamespace(el); } } But it doesn't work. When I debug inside the method, attribute is removed, but when the method is fully executed, no changes were saved. The document is

Merge XElement into XDocument and resolve namespaces

本小妞迷上赌 提交于 2019-12-31 03:36:07
问题 Given the following XDocument , initialized into variable xDoc : <Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"> <ReportSection> <Width /> <Page> </ReportSections> </Report> I have a template embedded in an XML file (let's call it body.xml ): <Body xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"> <ReportItems /> <Height /> <Style /> </Body> Which I would like to put as a child of <ReportSection> . Problem is if add

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

Get the EndElement Node of an XElement

▼魔方 西西 提交于 2019-12-25 07:37:41
问题 I am trying to get the End node of an XElement in a C# Console application. Say for example my XML Content is: <Object> <Item>Word</Item> <Value>10</Value> </Object> How can I fetch </Object> as a node? On iterating through element.DescendantNodesAndSelf() I still don't see isolated </Object> node. static void Main(string[] args) { const string xmlString = "<Object><Item>Word</Item><Value>10</Value></Object>"; var element = XElement.Parse(xmlString); foreach (var node in element

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(