-2
XElement service = doc.Element("Ids");
service.Add(new XElement("ID", idulong.ToString(),new XElement("Succesfull",1)));

Example output

<ID>
9223372036854775808
<Succesfull>1</Succesfull>
</ID>`

I want

<Ids Userid= 9223372036854775808>  
<Succesfull>1</Succesfull>
</Ids>

The Id is different for each user.I want check users with id.

3 Answers3

1

Look at your code just changed to show the level of nesting:

service.Add(new XElement("ID",
                         idulong.ToString(),
                         new XElement("Succesfull",
                                      1)));

In other words, the Succesfull element is within the ID element constructor call.

You just want to add two separate elements, either with two separate calls to service.Add:

service.Add(new XElement("ID", idulong.ToString());
service.Add(new XElement("Succesfull", 1));

or in one call to Add, taking multiple elements:

service.Add(new XElement("ID", idulong.ToString()),
            new XElement("Succesfull", 1));

You don't need to call ToString, by the way - it's fine to write:

service.Add(new XElement("ID", idulong),
            new XElement("Succesfull", 1));

(You might want to use Successful or just Success instead of Succesfull though...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thx for your answer but this doesn't work because i try a create user.This codes create different elements i want create element(for each user) and apply elements.This is my bad i didn't create a true question – user4126354 Mar 11 '15 at 16:02
  • @user4126354: Sorry, I don't understand your comment at all. This will create the XML you said you wanted. If that's not *actually* what you want, you should be clearer in your question. What do you mean by "apply elements"? – Jon Skeet Mar 11 '15 at 16:04
  • Oh thx for the all answers.i want check user with id element afaik i'm need aply attribute for the id. Is this true? – user4126354 Mar 11 '15 at 16:10
  • @user4126354: I have no idea what you mean by that. *Please* take the time to actually clarify what you mean. – Jon Skeet Mar 11 '15 at 16:19
  • @JonSkeet - I strongly suspect that invalid XML `` shown in the question meant to demonstrate "how to create an element with UserId attribute" (which look like already answered some time ago http://stackoverflow.com/questions/5063936/how-to-put-attributes-via-xelement)... Not really sure why `Ids` element would have `UserId` attribute so... – Alexei Levenkov Mar 11 '15 at 16:22
  • @AlexeiLevenkov: Ah - that's very different from the original question. The OP has edited the question, but not actually mentioned it... – Jon Skeet Mar 11 '15 at 16:25
0

Try this:

XElement service = doc.Element("Ids");
service.Add(
    new XElement("ID", idulong.ToString()),
    new XElement("Succesfull", 1)
);
rtf_leg
  • 1,789
  • 1
  • 15
  • 27
0
XElement service = doc.Element("Ids");
service.Add(new XElement("ID", idulong.ToString()),
        new XElement("Succesfull", 1));

not

service.Add(new XElement("ID", idulong.ToString(),new XElement("Succesfull",1)));
Hazem Abdullah
  • 1,837
  • 4
  • 23
  • 41