cXml how to add tag using predefined class

224 Views Asked by At

I have problem with adding tag element to cXml document. I'm using predefined class for serialization from cxml.org. I want to add tag named SharedSecret to existing document. It should look like:

<Header>
<From><Credential domain="AribaNetworkUserId">
<Identity>[email protected]</Identity>
</Credential>
</From>
<To>
<!-- Recipient -->
<Credential domain="AribaNetworkUserId">
<Identity>[email protected]</Identity>
</Credential>
</To>
<Sender>
<!-- Sender -->
<Credential domain="AribaNetworkUserId">
<Identity>[email protected]</Identity>
<SharedSecret>abracadabra</SharedSecret>
</Credential>
<UserAgent>Sender Application 1.0</UserAgent>
</Sender>
</Header>

And I have clasess:

    public partial class Header
    {

        /// <remarks/>
        public From From;

        /// <remarks/>
        public To To;

        /// <remarks/>
        public Sender Sender;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("Node", IsNullable = false)]
        public Node[] Path;

        /// <remarks/>
        public OriginalDocument OriginalDocument;
    }

 public partial class Node
        {

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Credential")]
            public Credential[] Credential;

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public NodeType type;

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public NodeItemDetailsRequired itemDetailsRequired;

            /// <remarks/>
            [System.Xml.Serialization.XmlIgnoreAttribute()]
            public bool itemDetailsRequiredSpecified;
        }
 public Identity Identity;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("CredentialMac", typeof(CredentialMac))]
        [System.Xml.Serialization.XmlElementAttribute("DigitalSignature", typeof(DigitalSignature))]
        [System.Xml.Serialization.XmlElementAttribute("SharedSecret", typeof(SharedSecret))]
        public object Item;
...

public partial class SharedSecret
        {

            /// <remarks/>
            [System.Xml.Serialization.XmlTextAttribute()]
            [System.Xml.Serialization.XmlAnyElementAttribute()]
            public System.Xml.XmlNode[] Any;
        }

And I have no idea what how to add this tag to document. I spent to much time trying to adding some Xmlnodes element and stuffs like this. The most difficult thing that I couldn't grasp is the SharedSecret class where I have one field and I have to add some string insted of another XmlNode.

the all cXml classes are available http://212.59.240.129/upload/cxml.txt

Please help me.

1

There are 1 best solutions below

4
jdweng On BEST ANSWER

Here is my test code :

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode[] identity = new XmlNode[] { doc.CreateTextNode("[email protected]")};
            XmlNode[] sharedSecret = new XmlNode[] { doc.CreateTextNode("abracadabra") };

            Header header = new Header()
            {
                Sender = new Sender()
                {
                    Credential = new Credential[] {
                        new Credential() { 
                            domain = "AribaNetworkUserId",
                           Identity = new Identity() { Any =  identity },
                           Item = new SharedSecret() { Any = sharedSecret }
                        }
                    }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(Header));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, header);
            writer.Flush();
            writer.Close();

        }

Here is Xml

<?xml version="1.0" encoding="utf-8"?>
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Sender>
    <Credential domain="AribaNetworkUserId">
      <Identity>[email protected]</Identity>
      <SharedSecret>abracadabra</SharedSecret>
    </Credential>
  </Sender>
</Header>