1

is it possible to have a declarative markup for a C# object model in order to generate something like this:

 ...
 <locales>
    <add id="698" code="tr" name="Turkish" xsd:Transform="Insert"/>
    <add id="701" code="fr" name="French" />
 </locales>
 ....

instead of :

  ...
  <locales>
    <add d5p1:Transform="Insert" Locator="asdasdf" id="698" code="tr" name="Turkish" xmlns:d5p1="xdt" />
    <add id="701" code="fr" name="French" />
  </locales>
  ...

a simple example of my code is:

public class BaseTransformation
{
    [XmlAttribute]
    public string IsDefault { get; set; }

    [XmlAttribute(Namespace ="xdt")]
    public string Transform { get; set; }

    //[XmlAttribute("Locator")]
    public string Locator { get; set; }       
}


public class Locale : BaseTransformation
{
    [XmlAttribute("id")]
    public long ID { get; set; }

    [XmlAttribute("code")]
    public string Code { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }
}


public class Languages
{
    [XmlArray(ElementName = "locales")]
    [XmlArrayItem(ElementName = "add")]
    public Locale[] Locales { get; set; }
}

I am trying to generate dynamically web.config transformations. d5p1:Transform="Insert" xmlns:d5p1="xdt" those two are not recognizable on build and does not apply the same functionality as intended.

10
  • Just specify the namespace: msdn.microsoft.com/en-us/library/… Commented Sep 11, 2017 at 13:56
  • @DaveM That's what I did on property "Trasnform" ... Commented Sep 11, 2017 at 13:58
  • Private properties will not be included in xml. So make properties private. Commented Sep 11, 2017 at 13:59
  • The problem is it is serializing this -> d5p1:Transform="Insert" xmlns:d5p1="xdt"................. and I need only xdt:Transform="Insert" Commented Sep 11, 2017 at 14:04
  • @jdweng I am not having issues with which properties are being included Commented Sep 11, 2017 at 14:04

1 Answer 1

2

You are confusing XML namespace prefixes with actual namespaces. In the Namespace property of XmlAttribute, you specify the full actual namespace, not the prefix you defined for that namespace. Namespace prefixes are arbitrary, and you can use whatever prefix you wish with a particular namespace using the xmlns attribute. The serialize thinks you are talking about a namespace called "xdt", not the actual namespace the "xdt" namespace typically refers to: "http://schemas.microsoft.com/XML-Document-Transform"

[XmlAttribute(Namespace = "http://schemas.microsoft.com/XML-Document-Transform")]
public string Transform { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

And I was indeed misinterpreting the two .. thus my confusion

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.