0

I've found some code to serialize classes to XmlElement here: How do I convert a C# class to an XMLElement or XMLDocument More specifically, I used Dave Andersen's solution.

public static XmlElement SerializeToXmlElement(object o)
{
    XmlDocument doc = new XmlDocument();
    using(XmlWriter writer = doc.CreateNavigator().AppendChild())
    {
        new XmlSerializer(o.GetType()).Serialize(writer, o);
    }
    return doc.DocumentElement;
}

I realized however, that when I use the GetElementById-function on the created document, the result is always null. After some snooping around, I found that XmlDocument uses an internal list named _htElementIdMap. This list seems to not be initialized.

So far I haven't been able to figuere out what I'm doing wrong, or wether I'm maybe trying to do something that is not supported. Does anybody know how I can fix this?

Thank you in advance

Edit:

The simplified version of what I do is the following

public static void Main(String[] args)
{
    var timestamp = new TimestampType
    {
        Id = "Test1",
        Created = new AttributedDateTime
        {
            Value = DateTime.Now.ToUniversalTime().ToString("o")
        },
        Expires = new AttributedDateTime
        {
            Value = DateTime.Now.AddDays(1).ToUniversalTime().ToString("o")
        }
    };
    var timestampXmlElement = SerializeToXmlElement(timestamp);

    var result1 = timestampXmlElement.OwnerDocument.GetElementById("Test1");
}

public static XmlElement SerializeToXmlElement(object o)
{
    XmlDocument doc = new XmlDocument();
    using (XmlWriter writer = doc.CreateNavigator().AppendChild())
    {
        new XmlSerializer(o.GetType()).Serialize(writer, o);
    }
    return doc.DocumentElement;
}

the problem is that result1 is null.

this is the xml for timestampXmlElement :

<Timestamp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" p3:Id="Test1" xmlns:p3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <p3:Created>2018-06-12T12:58:20.5213777Z</p3:Created>
    <p3:Expires>2018-06-13T12:58:20.5249304Z</p3:Expires>
</Timestamp>
pieterVdM
  • 34
  • 5
  • [XY problem](https://meta.stackexchange.com/q/66377/367909)? – Alexander Petrov Jun 01 '18 at 14:44
  • You are probably having issue with namespaces. You need to look at the string version of the xml to be able to find solution. Use the ToString() method and post samples of the xml to get solution. – jdweng Jun 02 '18 at 05:32
  • @AlexanderPetrov the code I'm writing is supposed to create signed soap-envelopes. It crashes in the System.Security.Cryptography.Xml.SignedXml class on the ComputeSignature() function. I fuguered out that this is where GetElementById is being called. that is why I ask here if anyone knows why this is not working. – pieterVdM Jun 05 '18 at 08:50
  • @jdweng do you mean the xml-namespaces? The C# classes that I'm converting are all genereated by the xsd-tool, based on the schemas I've found online. And I've had this issue, even when converting the bare minimum of some of the classes. – pieterVdM Jun 05 '18 at 08:52
  • Yes. You are converting the classes to an XmlDocument ( or XmlElement) that will contain the namespaces created by the xsd tool. Then you are parsing the results with GetElementById which requires the namespace. What namespace are you using with GetElementById? Are you getting any exceptions? Either the xml that is being generated is bad an you are getting an exception or the GetElementById need the namespace. – jdweng Jun 05 '18 at 10:12
  • thank you already for your help, and sorry for the late response. I haven't been given a lot of time to continue on this lately. I've updated my question to add some sample code. I didn't add the TimestampType-class, cause it's a bit of code, if it would be usefull for anyone just let me know and I'll add it. – pieterVdM Jun 12 '18 at 10:26

0 Answers0