linq-to-xml

how to remove nodes in an xml that is inside another xml

 ̄綄美尐妖づ 提交于 2020-01-17 13:48:25
问题 I have an xml as an value of an element inside a Main xml. I want to scrub off or delete a node within the inner xml. How do I achieve that ? For removing a node in main xml I am doing var result = doc.Descendants("node1").Where(x => x.Attribute("id").Value == "002"); if (result != null) { result.Remove(); } Here is my XML : <?xml version="1.0" encoding="utf-16"?> <root> <node1>id="001" version="1.0"</node1> <node2>id="002" version="1.0"</node1> <report>raw = "<response = "10"><innerxml><prod

how to remove nodes in an xml that is inside another xml

陌路散爱 提交于 2020-01-17 13:47:01
问题 I have an xml as an value of an element inside a Main xml. I want to scrub off or delete a node within the inner xml. How do I achieve that ? For removing a node in main xml I am doing var result = doc.Descendants("node1").Where(x => x.Attribute("id").Value == "002"); if (result != null) { result.Remove(); } Here is my XML : <?xml version="1.0" encoding="utf-16"?> <root> <node1>id="001" version="1.0"</node1> <node2>id="002" version="1.0"</node1> <report>raw = "<response = "10"><innerxml><prod

NullReferenceException while using XElement

二次信任 提交于 2020-01-17 07:28:12
问题 I have a method that is suppose to edit a xml file: public void EditItem(Item item, string xml) { Data = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Items/" + xml + ".xml")); XElement node = Data.Root.Elements("item").Where(i => (string)i.Element("ID") == item.ID).FirstOrDefault(); node.SetElementValue("ID", item.ID); node.SetElementValue("Name", item.Name); node.SetElementValue("Type", item.Type); node.SetElementValue("Kr", item.Kr); node.SetElementValue("Euro", item.Euro);

adding groups of children to random empty node

不羁的心 提交于 2020-01-17 06:18:24
问题 I am attempting to create an xml document as a save structure for my program using XDocument. creating the document, and seeding the known data is fine, but when it comes to creating data into the document from a list of struct(s) I run into a set of issues I am getting 'NullReferenceException' when trying to access my node created specifically for insertion. there is no clear documentation on adding children to a random empty node. everything I have found so far is on adding known values to

Get XElement by value of its attribute

*爱你&永不变心* 提交于 2020-01-16 05:09:12
问题 I've got the following XML: <rootNode> ... some stuff <ReportCellRef> <dang n="DVCompany" h="0" u="0" o="0" fmt="0"> ... some stuff </dang> </ReportCellRef> </rootNode> And I want get the <dang ...> ... </dang> node as XElement, so I can replace it with another node, provided I have the value of the n attribute. I've got this code: Dim nameToSearch = importNode.Attribute("n").Value Dim replaceable = From dangToTake In xdoc.Elements("ReportCellRef") _ Where CStr(dangToTake.Element("dang")

LINQ To XML Syntax for XML Element with Attributes

此生再无相见时 提交于 2020-01-15 05:01:25
问题 I'm a bit of a LINQ newbie and I was having some trouble with the following. I'm trying to perform a query using LINQ on an XML file and store the results in a list of DataClass objects that match the XML. I've got an XML file that is defined like this: <NewDataSet> <NewDataTable> <Field>Accepted ASNs</Field> <Val>59</Val> <Order Number="1234" ShipDate="2009/05/21" /> <Order Number="2190" ShipDate="2009/05/22" /> <Order Number="1809" ShipDate="2009/05/22" /> </NewDataTable> <NewDataTable>

How to read XML file in c#?

泄露秘密 提交于 2020-01-14 10:09:29
问题 I have following XML file, i want to know best way to read this XML file <MyFile> <Companies> <Company>123</Company> <Company>456</Company> <Company>789</Company> </Companies> </MyFile> As an output i need collection of values like "123,456,789" or it could be array of string[] Can we use Linq to xml? How? 回答1: var xdoc = XDocument.Load(PATH_TO_FILE); var companies = xdoc.Descendants("Company").Select(c => (string)c).ToArray(); This will give you a string[] . 回答2: Use LINQ to XML, Include

How to read XML file in c#?

烂漫一生 提交于 2020-01-14 10:08:43
问题 I have following XML file, i want to know best way to read this XML file <MyFile> <Companies> <Company>123</Company> <Company>456</Company> <Company>789</Company> </Companies> </MyFile> As an output i need collection of values like "123,456,789" or it could be array of string[] Can we use Linq to xml? How? 回答1: var xdoc = XDocument.Load(PATH_TO_FILE); var companies = xdoc.Descendants("Company").Select(c => (string)c).ToArray(); This will give you a string[] . 回答2: Use LINQ to XML, Include

Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name

回眸只為那壹抹淺笑 提交于 2020-01-14 07:17:06
问题 This is my Entity Class with an Entity : [Table(Name = "CLINICAL_ITEM_MASTER")] public class ClinicalItemMaster { [Column] public int CLIENT_INPUT_MHS_ID { get; set; } [Column] public Guid CLIENT_INPUT_MHS_GUID { get; set; } [Column] public string ITEM { get; set; } [Column] public int ITEM_ID { get; set; } [Column] public string ITEM_NUMBER { get; set; } [Column] public string CATEGORY { get; set; } [Column] public string DESCRIPTION { get; set; } [Column] public DateTime? CREATE_DTTM { get;

Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name

人走茶凉 提交于 2020-01-14 07:16:01
问题 This is my Entity Class with an Entity : [Table(Name = "CLINICAL_ITEM_MASTER")] public class ClinicalItemMaster { [Column] public int CLIENT_INPUT_MHS_ID { get; set; } [Column] public Guid CLIENT_INPUT_MHS_GUID { get; set; } [Column] public string ITEM { get; set; } [Column] public int ITEM_ID { get; set; } [Column] public string ITEM_NUMBER { get; set; } [Column] public string CATEGORY { get; set; } [Column] public string DESCRIPTION { get; set; } [Column] public DateTime? CREATE_DTTM { get;