FAQ Database Discussion Community


How does one increment/update integer value of an XML Attribute in C# (XAttribute)?

c#,xml,linq,xelement,xattribute
TL;DR: If I have an XAttribute of NumFruits in an XElement, how can I increment/update the value from 0 to 1,2,3... ? The issue: When I attempt to increment the XAttribute like so: basket.Attribute("numFruits").Value += 1 the result for numFruits will be numFruits = 01 (since 0 was the initial...

How I can collect similar xml tag to groups with Xml.Linq in c#?

c#,xml,linq,linq-to-xml,xelement
I have a xml file with this style <all_elements> <elements> <element attribute="7" /> <element attribute="1" /> <element attribute="6" /> </elements> <elements> <element attribute="2" /> <element attribute="8" /> </elements> . . . </all_elements> I want calculate average of each elements seprate with c# Xml.Linq, for exapmle: average1= 4.66 average2= 5.00 I...

Does XElement have built in support for nil=true

c#,xelement
I have the following xml parsed into an XElement named entry. <Person> <Name>Ann</Name> <Age i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" /> </Person> When fetching the age property I write this: var entry = XElement.Parse( "<Person><Name>Ann</Name><Age i:nil=\"true\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" /></Person>"); var age = entry.Element("Age").Value; age is now "", and I wonder if there is some kind...

How to write List items into an xml file? [duplicate]

c#,xml,linq,list,xelement
This question already has an answer here: Serializing a list of objects to XDocument 2 answers I need to write items in the List<int> myList into an xml file. The way the xml file should look like is if the values in the list are 1, 2 and 3....

select distinct xelement based on attribute

xml,linq,lambda,distinct,xelement
<field name="value" value="A" /> <field name="value" value="B" /> <field name="text" value="C" /> <field name="text" value="D" /> <field name="key" value="E" /> When I filter I need to get one element for "value", one element for "text" and one element for "key" (Yes, "key" has only one element and it should...

How to select a value depending on 2 attributes from separate nodes using XPath in C#?

c#,xml,xpath,attributes,xelement
So, I have an XML file like the following structure, <Root> <Parent StoreDate="2014-12-31" Type="1"> <Child1>2014-01-31</Child1> <Child2 TimePeriod="M1"> <GrandChild1>-4.58849</GrandChild1> <GrandChild2>288</GrandChild2> </Child2> </Parent> <Parent StoreDate="2014-12-31" Type="1"> <Child1>2014-02-28</Child1> <Child2 TimePeriod="M1"> <GrandChild1>4.58015</GrandChild1> <GrandChild2>284</GrandChild2>...

Getting address data from Google Maps XML

c#,xml,xelement
I've extracted some addresses from google maps and they are in an xml file. In myxml file I have some xelements like <location>, <place_id>, <adr_address>, etc The 'adr_address' element has different classes and each class contains, city, street, country, etc. values. How do I get each value from the 'adr_address'...

XML node Latest timestamp C#

c#,xml,linq,duplicates,xelement
I need to only print the same employee once and check if its the latest timestamp. I need employee id only once but the latest timestamp of it. This is my result so far, printing same id with different timestamps (example): 1 2015-03-16T21:32:30 1 2015-03-16T21:33:30 2 2015-03-16T21:32:30 3 2015-03-16T21:32:30 2...

XElement.Value is stripping XML tags from content

c#,.net,xml,xml-parsing,xelement
I have the following XML: <Message> <Identification>c387e36a-0d79-405a-745c-7fc3e1aa8160</Identification> <SerializedContent> {"Identification":"81d090ca-b913-4f15-854d-059055cc49ff","LogType":0,"LogContent":"{\"EntitiesChanges\":\" <audit> <username>acfc</username> <date>2015-06-04T15:15:34.7979485-03:00</date> <entities> <entity> <properties> <property> <name>DepId</name> <current>2</current> </property>...

How to serialize object with List inside in C# using XElement?

c#,xml,serialization,xelement
I have object with members of different types like this: public class MyObject { public string Str1 = string.Empty; public MyEnums.Enum1 E1 = MyEnums.Enum1.Unknown; public bool Done = false; }; I have Dictionary of these objects: Dictionary<string, MyObject> MyObjectsDic = new Dictionary<string, MyObject>(); And serializer for it like this: public...

Cant go deeper than root, LINQ to (Funds)XML, C#

c#,linq-to-xml,xelement
I'm working with a specific FundsXML-Schema trying to get all Assetss of a specific XML-File to iterate through. Short example of xml-file: <?xml version="1.0" encoding="utf-8"?> <FundsXML xmlns="http://www.fundsxml.org/XMLSchema/3.0.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="3.0.5" xsi:schemaLocation="http://www.fundsxml.org/XMLSchema/3.0.5 FundsXML3.0.5.xsd"> <Date>2015-02-27</Date> ... <AssetMasterData> <Asset> <SecurityCodes> <ISIN>XXXXXXXXXXXX</ISIN>...

Get XElement by value of its attribute

vb.net,linq-to-xml,xelement
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...

Accessing XElement with specific value

c#,xml,linq,linq-to-xml,xelement
I am trying to save my battleship game to XML and so far it has gone pretty good. What I'm saving in the XML file is player names, coordinates for ships and misses. The ID attribute for the ships is to know which ship coordinates makes a whole ship. Next...