0

I have an object that is supposed to describe some customer with these values:

{
    public class CustomerDescription
    {
        public string FirstName { get; set; } = "";
        public string LastName { get; set; } = "";
        public string StreetName { get; set; } = "";
        public string HouseNumber { get; set; } = "";
        public string PostalPlace { get; set; } = "";
        public string PostCode { get; set; } = "";
        public string CountryCode { get; set; } = "";
        public string EMail { get; set; } = "";
        public string PhoneNumber { get; set; } = "";
    }
}

These values are retrieved from a database of customers and will be used to create an XML file which will be sent via SOAP to another database. However, not all these values are always present. For example one customer may not have the country code in the database. In this case I would like to not include this value at all in the XML file I am sending. Here is an example of how I create the XML elements:

new XElement("address",
           new XElement("address1", CustomerDescription.StreetName + " " + cabCustomerDescription.HouseNumber),
           new XElement("postalCode", cUstomerDescription.PostCode),
           new XElement("city", customerDescription.PostalPlace),
           new XElement("countryCode", customerDescription.CountryCode))
)

This is done for all the properties in CustomerDescription. My question is, how can I do this so that if a value is not present in the database, then this value is not included in the XML file? I would not like it to be empty, like <countryCode></countryCode>, but rather not present at all.

PalBo
  • 2,203
  • 3
  • 22
  • 43
  • 1
    Why are you trying to create the SOAP payload by hand instead of simply creating and using a service proxy? SOAP works with XML requests that have a very specific, well defined (actually *standardized*) form. Not XML files – Panagiotis Kanavos Dec 10 '19 at 10:47
  • In Visual Studio you can create a proxy by right-clicking on `References`, select `Add Service Reference` and add the URL to the SOAP service's WSDL – Panagiotis Kanavos Dec 10 '19 at 10:49
  • I first used the WCF client, and ran into a problem with attaching a client certificate to the client I was creating. After not being able to solve this problem for about 2 weeks (I posted several times on stackoverflow as well) I decided to do it this way temporarily. – PalBo Dec 10 '19 at 11:37
  • so now you're going to reimplement WCF? WCF works - since 2008. HTTPS works out of the box. WS-Security works. Signing works. If your previous questions got no answer it's probably because they were too vague or unclear. You *don't* need to write a ton of code to use a certificate. If you only need to use HTTPS, you don't really have to write any code. Just use a different binding *in the configuration* – Panagiotis Kanavos Dec 10 '19 at 11:41
  • and the updates in [this question](https://stackoverflow.com/questions/58969598/wcf-service-unable-to-establish-trust-relationship-for-the-ssl-tls-secure-channe) are a *VERY* clear indication that the infrastructure you use has serious problems - unsigned certificates and bad clocks typically typically mean a MITM attack. It's HTTPS that refuses the connection, not WCF. If you end up disabling every single HTTPS check that's meant to protect you, what's the point of using HTTPS at all? – Panagiotis Kanavos Dec 10 '19 at 11:45
  • Regarding the unsigned certificate - that's what a porcupine or a hacker's proxy server would do to implement a MITM attack. That's why certificate validation is used, to prevent the use of untrusted certificates or proxies acting as if they were the real target – Panagiotis Kanavos Dec 10 '19 at 11:47
  • Regarding the clocks - refusing to accept calls with invalid timestamps prevents replay attacks. In a Windows domain, all machines synchronize with the domain controller, which in turn syncs with one or more NTP servers. Machines outside a domain, or Linux, Mac OS machines, sync directly with NTP servers. How come your server and client end up with such a skew that prevents HTTPS from working? Just fix the clocks – Panagiotis Kanavos Dec 10 '19 at 11:49
  • Regarding the different binding, I also figured out that this may be the solution switching from BasicHttpBinding to WShttpbinding. I could however not get wshttpbinding to work with .NET core 2.2 which is what I have to use as other things in my project depends on it, Azure Functions and DevOps among other things. – PalBo Dec 10 '19 at 11:52
  • you never mentioned .NET Core in your questions or tags. You don't need WSHttpBinding, just BasicHttpBinding with Transport security will work. The `Connected Services` proxy should handle all this – Panagiotis Kanavos Dec 10 '19 at 11:57
  • I agree that it should work, but the service provider can confirm that my messages were being delivered without any certificate attached to them. – PalBo Dec 10 '19 at 12:04
  • Certificates contain the public/private keys used to encrypt and sign documents. They aren't attached to anything. Are you looking for WS-Security signing? You should post a *clear* question explaining what you actually want to do,not how you tried to do it. Other people probably encountered the same problems already. – Panagiotis Kanavos Dec 10 '19 at 12:37
  • 1
    You should probably create a proxy with .NET Framework too, and try simply *copying* the code. Most of the problems are caused by the proxy generator. The generated code uses classes that are more-or-less available. You may have to use a `CustomBinding` instead of `WSHttpBinding`. YOu could also create the proxy in a .NET Framework library and just add a reference to the library. .NET Core will issue warnings but you can ignore them as long as your code runs on Windows. I'm using [AlphaFS](https://www.nuget.org/packages/AlphaFS/) in .NET Core apps this way – Panagiotis Kanavos Dec 10 '19 at 12:38
  • I'll give it a try, thanks. – PalBo Dec 10 '19 at 14:08

1 Answers1

0

You just need to use ternary operator if value is null from database dont include in x element.

Ex:Explicitely i did StreetName null an dcheck if null then dont add in xml file.

CustomerDescription t = new CustomerDescription();
 t.StreetName = null;
            var abc = new XElement("address", t.StreetName != null ? new XElement("address1", t.StreetName + " " + t.HouseNumber) : null,
            new XElement("postalCode", t.PostCode),
            new XElement("city", t.PostalPlace),
            new XElement("countryCode", t.CountryCode));
Gagan Burde
  • 328
  • 2
  • 13