0

I have a class, created from Agresso's XSD using xsd.exe, which I successfully use to generate my XML file. I define an object ABWInvoice oInvoiceAgresso = new ABWInvoice() { }; , populate with relevant data and then save as an XML file, using the following method.

public static void SaveXml<ObjectType>(ObjectType o, string fileName)
    {
        using (var sw = new StreamWriter(myDecodePath(fileName)))
        {
            new XmlSerializer(typeof(ObjectType)).Serialize(sw, o);
        }
    }

The XML file has lots of namespaces, e.g.

<InvoiceDate xmlns="http://services.agresso.com/schema/ABWSchemaLib/2011/11/14">2018-02-05</InvoiceDate>

So far all well, but a customer now requires to remove the namespaces across the XML, i.e. to present only <InvoiceDate>2018-02-05</InvoiceDate>

There are lots of examples on the Net how to do it with an XML file, meaning I will have to continue saving my object as an XML file and then to reload it for processing.

I wonder if there is any better and I think also faster approach to do it directly on my oInvoiceAgresso object, rather than saving it first, please?

We use VS 2017.

Community
  • 1
  • 1
KDWolf
  • 398
  • 2
  • 14
  • This is probably not what you want to hear, but it sounds like your customer can't process fully formed/correct XML, which is really their problem not yours. Using a 'proper' XML parser is key to ensure data integrity, and the days of rolling your own parser (which it sounds like your customer has done) are long gone. – Neil Feb 21 '18 at 09:32
  • Agree, yet I still need to address the request. :) – KDWolf Feb 21 '18 at 10:51
  • Possible duplicate of [How to serialize an object to XML without getting xmlns="..."?](https://stackoverflow.com/questions/258960/how-to-serialize-an-object-to-xml-without-getting-xmlns) – Neil Feb 21 '18 at 11:52
  • I would (politely) tell the customer that you are producing standards compliant output, and they need to process it properly. It's possible (if your xml is complicated enough), that removing the namespacing, will screw up other customers importing the same data. Namespacing is there for a reason. – Neil Feb 21 '18 at 11:54
  • Anyway, I believe this answer: https://stackoverflow.com/questions/258960/how-to-serialize-an-object-to-xml-without-getting-xmlns might do the trick. – Neil Feb 21 '18 at 11:55
  • Thank you, Neil. I have tried [How to serialize](https://stackoverflow.com/questions/258960/how-to-serialize-an-object-to-xml-without-getting-xmlns) with two options `xmlns.Add(string.Empty, string.Empty);` or `xmlns.Add("", "");` but it didn't remove the namespace. – KDWolf Feb 21 '18 at 14:30
  • Have also tried to use `xmlns.Add("agrlib",string.Empty);` as per [Agresso XSD](http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.html#element_InvoiceDate_Link06799B30), but to no avail - had an error message, that such a space doesn't exist at all. – KDWolf Feb 21 '18 at 14:54

0 Answers0