1

I need to generate an XML document that follows this specifictaion

<productName locale="en_GB">Name</productName>

but using XMLSeralization I am getting the following

<productName locale="en_GB">
<Name>Name</Name>
</productName>

My C# code is like this:

[Serializable]
public class productName
{
    public productName()
    {

    }

    public string Name;
    [XmlAttribute]
    public string locale;
}

XmlAttribute is what is required to show the locale in the correct place, but I am unable to figure out how to export the Name field correctly.

Does anyone have an idea?

Thanks

EDIT:

This is the code to generate the XML

public static class XMLSerialize
{
    public static void SerializeToXml<T>(string file, T value)
    {
        var serializer = new XmlSerializer(typeof(T));
        using (var writer = XmlWriter.Create(file))
            serializer.Serialize(writer, value);
    }

    public static T DeserializeFromXML<T>(string file)
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(T));
        TextReader textReader = new StreamReader(file);
        T result;
        result = (T)deserializer.Deserialize(textReader);
        textReader.Close();

        return result;
    }
}
4
  • what method are you using to generate the XML from the class? Commented Sep 8, 2015 at 10:48
  • Added the xml serialize code to the question Commented Sep 8, 2015 at 10:51
  • 1
    is productName root element of your XML? Commented Sep 8, 2015 at 10:52
  • No the XML is much larger, this is just one element that has the issue. Commented Sep 8, 2015 at 10:52

2 Answers 2

5

Instead of specifying Name as element specify it as text value by adding [XmlText] attribute

[XmlText]
public string Value { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

2

This contains not only a direct answer to your question, but more of a indirect answer of how to solve similar issues like this in the future.

Start the other way around, with your xml, write your xml exactly like you want it and go from there, like this:

// assuming data.xml contains the xml as you'd like it
> xsd.exe data.xml             // will generate data.xsd, ie xsd-descriptor
> xsd.exe data.xsd /classes    // will generate data.cs, ie c# classes
> notepad.exe data.cs          // have a look at data.cs with your favorite editor

Now just have a look at data.cs, this will contain an enormous amount of attributes and stuff and the namespaces are probably wrong, but at least you know how to solve your particular xml-issue.

The direct answer is to use the XmlTextAttribute on the given property, preferably named Value since that is the convention I've seen so far.

[Serializable]
public class productName {
 public productName() { }

 [XmlText]
 public string Value {get; set;}

 [XmlAttribute]
 public string locale {get; set;}
}

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.