0

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>sender@sendercompany.com</Identity>
</Credential>
</From>
<To>
<!-- Recipient -->
<Credential domain="AribaNetworkUserId">
<Identity>recipient@recipientcompany.com</Identity>
</Credential>
</To>
<Sender>
<!-- Sender -->
<Credential domain="AribaNetworkUserId">
<Identity>sender@sendercompany.com</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.

KrisJuris
  • 3
  • 3
  • First, The classes are not valid that you posted. For example the To and From object do not exist. So where did you get the classes from? You do not need to use serialization to do this task. There are other Net methods to add two xml documents together that may be simpler. – jdweng Feb 18 '18 at 11:12
  • I pasted only few classes from many available. http://212.59.240.129/upload/cxml.txt – KrisJuris Feb 18 '18 at 12:27
  • Shared secret is an attribute like Domain so it would look like this : If this does work create classes with sample data and serialize. Then look at the serialize results. – jdweng Feb 18 '18 at 15:02
  • @jdweng It's not an attribute...OP has it right. – MrGadget Feb 19 '18 at 15:05

1 Answers1

0

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("sender@sendercompany.com")};
            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>sender@sendercompany.com</Identity>
      <SharedSecret>abracadabra</SharedSecret>
    </Credential>
  </Sender>
</Header>
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This isn't right. The OP has shown correctly that SharedSecret is an element with inner text, not an attribute. You want `doc.CreateNode(Xml.XmlNodeType.Text, "", "")` and `sharedSecret[0].InnerText = "abracadabra"` instead. – MrGadget Feb 19 '18 at 15:02
  • It is defined in the Identity Class as object identity and tagged with XmlElementAttribute. Did you look at the URL with all the classes? – jdweng Feb 19 '18 at 16:25
  • I'm quite familiar with CXML. Look up what XmlElementAttribute, aka XmlElement means. – MrGadget Feb 19 '18 at 16:50
  • I fixed the code to get SharedSecret as an Element. – jdweng Feb 19 '18 at 17:20