Dennis Pichlmeier Dennis Pichlmeier - 2 months ago 8
C# Question

Create XML File like my Example

I would like to Create an XML File. But now my problem is to create the following XML attribute:

<MsrProgram OwnerTypeFullName="myData" Number="0">
<MsrRange2Width>4</MsrRange2Width>
</MsrProgram>


My problem are how to create the following attributes:
OwnerTypeFullName="myData"
Number="0"

Here is my XML Class:

public class MsrProgram
{
/// <summary>
/// Gets or sets the MsrRange2Width.
/// </summary>
/// <remarks>
/// Value stored in ....
/// </remarks>
[XmlElementAttribute(ElementName = "MsrRange2Width")]
public string MsrRange2Width { get; set; }

}


Thanks!

Answer

Using Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement msrProgram = new XElement("MsrProgram", new object[] {
                new XAttribute("OwnerTypeFullName","myData"),
                new XAttribute("Number", 0),
                new XElement("MsrRange2Width",4)
            });


        }
    }
}