0

How can I have whitespaces on XAttribute, here is my code snippet:

new XElement("SubstitutionAttribute", new XAttribute("SubNetwork Group",subNetBox.Text))

What I am trying to achieve in xml:

<SubstitutionAttribute name="SubNetwork Group" value="Something" />

Best regards,

Hugo

1 Answer 1

1

The reason your example is failing is because the constructor for XAttribute expects the name of the attribute and its value.

So with new XElement("SubstitutionAttribute", new XAttribute("SubNetwork Group",subNetBox.Text)) you are acutally declaring only one attribute with the name "Subnetwork Group" and the value subNetBox.Text (<SubstitutionAttribute SubNetwork Group="Something" />). This is invalid XML as you can't have spaces in an attribute name.

What I think you are trying to do should be done with two attributes - one called name and the other called value:

new XElement("SubstitutionAttribute", 
    new XAttribute("name", "SubNetwork Group"), 
    new XAttribute("value", subNetBox.Text));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.