2

I want to build the following XML node from a class.

<Foo id="bar">some value</Foo>

How should my class definition be?

class Foo
{
   public string Value {set;get;}
   public string id{set;get;}
}

I believe i should put some XML attributes to these properties but not sure what they are.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
DarthVader
  • 52,984
  • 76
  • 209
  • 300

2 Answers2

9

Take a look at the attributes under the System.Xml.Serialization namespace for that. In your case, the class should look like the code below.

public class StackOverflow_8281703
{
    [XmlType(Namespace = "")]
    public class Foo
    {
        [XmlText]
        public string Value { set; get; }
        [XmlAttribute]
        public string id { set; get; }
    }
    public static void Test()
    {
        MemoryStream ms = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(typeof(Foo));
        Foo foo = new Foo { id = "bar", Value = "some value" };
        xs.Serialize(ms, foo);
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
    }
}

Update: added code to serialize the type.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • ok and how can i generate XML from this class after assigning the values? – DarthVader Nov 26 '11 at 21:24
  • 1
    Edited the answer with example. You'll use the `XmlSerializer` class for that. – carlosfigueira Nov 26 '11 at 21:28
  • thanks. is this an efficient way to generate XML? is there better/faster ways i should research? – DarthVader Nov 26 '11 at 21:31
  • 1
    It'll only be inefficient if you overuse it. The serializer is quite performant for most of the scenarios. If it becomes a problem, you can look into other alternatives, but don't try to optimize your code until you need it. – carlosfigueira Nov 26 '11 at 21:34
2

For C# and Visual Basic, there is no reason to do this by hand. Visual Studio includes a command line tool that will generate the class or xml schema for you. See http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.100).aspx for details.

Nick Zimmerman
  • 1,471
  • 11
  • 11
  • Really? Down votes because I recommend using the tool that exists instead of doing it by hand? – Nick Zimmerman Nov 26 '11 at 21:40
  • I agree - why down vote this? And even more, why down vote with no explanation. That is not helpful or polite. If you are working with a well defined schema (and if you aren't, why not - they are very helpful except in very simple case) then this is a great tool. It let's you update your schema in one place and regenerate the code - much better than manually updating code and schema. – Mike Goodwin Nov 26 '11 at 21:47
  • This tool is really great. I also can not understand why downvoting, so i gave you an upvote :) – dasheddot Nov 26 '11 at 21:56
  • I also don't understand the downvotes - I've used the tool in the past and although I don't like its output too much (its output tends to be too verbose), it's often a good starting point for handcrafting a simple class for the purpose. Upvoted it as well. – carlosfigueira Nov 27 '11 at 00:00