0

I get a set of 'Attributes' with default values and I want to create a string which is a part xml document as below(not a complete xml document). I want to have a 'tab' in between every attribute. I've found string concatenation is the workable option in this case. In .Net is there any better way of doing this?

<XmlNodes>
<ChildNode Attribute1 ="100"    Attribute2="200"    Attribute3 ="0"/>
<ChildNode Attribute1="100"     Attribute2="200"    Attribute3 ="0"/>
...
</XmlNodes>
3
  • Not to sound too difficult, but why would you want to do this? Commented Apr 30, 2012 at 7:53
  • @yamen: In one of our internal web applications there is a XMLText field which contains 100s of items. It's time consuming when a user going to create that XMLText field for the first time. So I am trying to create this xml script automatically with default values for the given attributes. Commented Apr 30, 2012 at 7:57
  • That's simple enough - but why does it have to line up with tabs and everything? Is that so it's simpler to edit? Perhaps the script can accept the input in another way (say CSV) and then convert to XML? Commented Apr 30, 2012 at 7:59

1 Answer 1

1

Here's a fun little hack that at least only resorts to string manipulation late in the piece:

var nodes = new XElement("XmlNodes");
foreach (var i in Enumerable.Range(1,10))
{
    nodes.Add(new XElement("ChildNode", 
        new XAttribute("Attribute1", 100), 
        new XAttribute("Attribute2", 200), 
        new XAttribute("Attribute3", 0)));
}

var result = nodes.ToString().Replace("\" A", "\"\tA"); // **" A** becomes **"\tA**

But I maintain that if this is about providing 'templates' for people to edit then you're best off getting them to edit it in a non-XML format (like CSV) and then convert to XML when you parse it.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.