Questions tagged [xattribute]

The xattribute tag is for questions about the C# (System.Xml.Linq) XAttribute class.

One MSDN reference page for the class is Xattribute Class (System.Xml.Linq).

50 questions
17
votes
2 answers

XAttribute default value if not existing on XElement

Is there an easier/better way to return a default value if a XAttribute on a XElement is not existing?: I'm trying to write this in a shorter way (cause it's a two-liner): var a = root.Attribute("testAttribute"); var v = (a == null ? "" :…
Mike
  • 339
  • 1
  • 4
  • 9
7
votes
1 answer

Adding xml:space to root element

I have a little problem that I thought was a no-brainer ... but alas ... I have some xml and all I want to do is to add the xml:space="preserve" to the root element using c#. I tried this: var rootElem = xDoc.Root; //…
Jesper Lund Stocholm
  • 1,973
  • 2
  • 27
  • 49
6
votes
2 answers

How to add more Attribute in XElement?

I have a data structure as under class BasketCondition { public List SkuList { get; set; } public string InnerBoolean { get; set; } } class Sku { public string SkuName { get; set; } public int Quantity { get;…
Niladri Biswas
  • 4,153
  • 2
  • 17
  • 24
4
votes
1 answer

Using XNamespace to create nicely formatted XML

I want to create a Xml file that looks something like this:                How can I accomplish this using XAttribute, XElement, XNamespace, and…
Steven
  • 4,826
  • 3
  • 35
  • 44
3
votes
2 answers

How to Remove specific attributes in XMLDocument?

In my C# codebase, I have a XMLDocument of the form: Hi How Are You
GilliVilla
  • 4,998
  • 11
  • 55
  • 96
2
votes
1 answer

Check whether a value in a list of type IEnumerable exist in another list of same type + LINQ to XML

How can I check whether a value of XTclist exist in Xrclist. XR : Full scope 02/28/2009
BumbleBee
  • 10,429
  • 20
  • 78
  • 123
2
votes
1 answer

XAttribute is not aware of sibling XAttribute which IsNamespaceDeclaration

I have to be able to parse both these xml-s:
nvirth
  • 1,599
  • 1
  • 13
  • 21
2
votes
1 answer

Is XML Elements Attribute value always data type of string?

When I create a XML Document using LINQ, when I add some XElement to Root element with some Attributes and when I read that document`s XElement using LINQ, the returned value of XAttributes.Value is string by default! In order to assign this value…
2
votes
1 answer

XAttribute generates bogus prefix

I am trying to generate a piece of xml data using linq to xml. XNamespace xsins = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"); XAttribute xsiniltrue = new XAttribute(xsins+"Exists", "true"); XElement elem = new…
droidlooms
  • 35
  • 5
1
vote
1 answer

XAttribute.SetValue converts "

In my application I'm using XAttribute.SetValue(...) to set the value of an xml-attribute. The value is provided by the user. I have the following methode to set the attribute-value: private void SetValue(XAttribute attribute, string input) { …
Tomtom
  • 9,087
  • 7
  • 52
  • 95
1
vote
2 answers

Sanitize XML Attribute Values

How can i easily sanitize the values I pass into the Value property of an XAttribute.
Dested
  • 6,294
  • 12
  • 51
  • 73
1
vote
1 answer

cannot add XAttribute into an XElement

I want to add an attribute into an element. And I wish the new added attribute to be the first attribute in the element. I used the AddFirst(), I got an error : "An attribute cannot be added to content." I don't know why? the following is my codes.…
peter
  • 1,009
  • 3
  • 15
  • 23
1
vote
1 answer

Adding elements to xml file in C# using XDocument

this is my current XML structure i use the following C# but get error when i try to excute public static void writeSub(string a,string b,string c) { …
Iakovl
  • 1,013
  • 2
  • 13
  • 20
1
vote
1 answer

XDocument getting Root Attributes values

How can I get the value of title1 from this string in a datatable using xDocument I tried descendants, XAttributes and all sorts...Maybe input is wrong but XDocument xml =…
insanepaul
  • 187
  • 2
  • 5
  • 17
0
votes
2 answers

Reading from an attribute that might be missing in a xelement.add method in C#

I am trying to create a XElement that reads from another XElement built from a file. Below is a sample of the code. My question is how do I code around a source attribute that might not be there? docHeader and invoice are XElements. When running…
Styxtb1598
  • 23
  • 2
1
2 3 4