none
Create a xml string according to the form RRS feed

  • Question

  • I have a xml form as below. How to serialize using C #. Please help me!

    <?xml version="1.0" encoding="utf-16"?>
    <Customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Customer>
          <Name>abc</Name>
          <Code>kh08</Code>
          <TaxCode />
          <Address>avc</Address>
          <BankAccountName>aaa</BankAccountName>
          <BankName>bgg</BankName>
          <BankNumber>1234555</BankNumber>
          <Email>abc@gmail.com</Email>
          <Fax>0129838</Fax>
          <Phone>01234567890</Phone>
          <ContactPerson>acn</ContactPerson>
          <RepresentPerson>ddd</RepresentPerson>
          <CusType>0</CusType>
        </Customer>
        <Customer>
          <Name>bca</Name>
          <Code>kh10</Code>
          <TaxCode />
          <Address>avc</Address>
          <BankAccountName>aaa</BankAccountName>
          <BankName>bgg</BankName>
          <BankNumber>1234555</BankNumber>
          <Email>abc@gmail.com</Email>
          <Fax>0129838</Fax>
          <Phone>01234567890</Phone>
          <ContactPerson>acn</ContactPerson>
          <RepresentPerson>ddd</RepresentPerson>
          <CusType>0</CusType>
        </Customer>
    </Customers>

    Wednesday, April 17, 2019 8:53 AM

All replies

  • Hello,

    See the following solution if the intent is to serialize a list of (looks like) Customer.

    https://stackoverflow.com/questions/178899/serializing-lists-of-classes-to-xml


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, April 17, 2019 9:37 AM
    Moderator
  • The XML you've provided *is* the serialization. What exactly are you trying to accomplish? Do you want to deserialize this back to a class (or, what looks like a list of a class called Customer)?

    Without understanding your intent, I'm not sure what to suggest ...


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, April 17, 2019 3:48 PM
    Moderator
  • Hi MaskedTextBox,

    Thank you for posting here.

    For your question, you could try the code below.

    XNamespace m = "http://www.w3.org/2001/XMLSchema-instance";
                XDocument doc = new XDocument(
                                              new XDeclaration("1.0", "utf-16", ""),
                                              new XElement("Customers", new XAttribute(XNamespace.Xmlns + "xsi", m), new XAttribute(XNamespace.Xmlns + "xsd", m),
                                                           new XElement("Customer",
                                                                         new XElement("Name", "abc"),
                                                                         new XElement("Code", "kh08"),
                                                                         new XElement("TaxCode"),
                                                                         new XElement("Address", "avc"),
                                                                         new XElement("BankAccountName", "aaa"),
                                                                         new XElement("BankName", "bgg"),
                                                                         new XElement("BankNumber", "1234555"),
                                                                         new XElement("Email", "abc@gmail.com"),
                                                                         new XElement("Fax", "0129838"),
                                                                         new XElement("Phone", "01234567890"),
                                                                         new XElement("ContactPerson", "acn"),
                                                                         new XElement("RepresentPerson", "ddd"),
                                                                         new XElement("CusType", "0")
                                                                       ),
                                                            new XElement("Customer",
                                                                         new XElement("Name", "abc"),
                                                                         new XElement("Code", "kh10"),
                                                                         new XElement("TaxCode"),
                                                                         new XElement("Address", "avc"),
                                                                         new XElement("BankAccountName", "aaa"),
                                                                         new XElement("BankName", "bgg"),
                                                                         new XElement("BankNumber", "1234555"),
                                                                         new XElement("Email", "abc@gmail.com"),
                                                                         new XElement("Fax", "0129838"),
                                                                         new XElement("Phone", "01234567890"),
                                                                         new XElement("ContactPerson", "acn"),
                                                                         new XElement("RepresentPerson", "ddd"),
                                                                         new XElement("CusType", "0")
                                                                        )
                                                           )
    
                                             );
    
                doc.Save("text2.xml");

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 6:10 AM
    Moderator