Avatar of GeneBe
GeneBe
Flag for United States of America asked on

C# code does not output the XML format i want.

My C# code does not output the XML format i want.
Here's the code:

[WebMethod(Description = "GetAddressTest")]
[return: XmlRoot(ElementName = "AddressTest")]
public string GetAddressTest(string memberNbr)
{
    XNamespace defaultNamespace = XNamespace.Get("http://tempuri.org/Member.xsd");
    XElement addressInfo = new XElement("AddressDetail");
    IList<string> addressList = new List<string> { "101 state st", "201 exit st", "301 entrance st" };
    var i = 0;
    foreach (string item in addressList)
    {
        i++;
        addressInfo.SetElementValue("Address", item[i]);
    }
    return addressInfo.ToString();
}

Open in new window

here's the output format i want:

<?xml version="1.0" encoding="UTF-8"?>
<MemberSpansTest xmlns="http://www.w3.org/2001/XMLSchema-instance">
</MemberSpansTest>
	<MemberSpansDetail> 
		<Address>101 state st</Address> 
	</MemberSpansDetail>
	<MemberSpansDetail> 
		<Address>201 exit st</Address> 
	</MemberSpansDetail>
	<MemberSpansDetail> 
		<Address>301 entrance st</Address> 
	</MemberSpansDetail>
</MemberSpansTest>

Open in new window

C#XML

Avatar of undefined
Last Comment
GeneBe

8/22/2022 - Mon
gr8gonzo

That's invalid XML...
GeneBe

ASKER
its's just input data so invalid XML is ok. it's the data format that I am focusing on.
gr8gonzo

Can you post what you're CURRENTLY getting as output for comparison?
Your help has saved me hundreds of hours of internet surfing.
fblack61
GeneBe

ASKER
Here's the output i get:
<?xml version="1.0" encoding="UTF-8"?>
<AddressTest xmlns="http://www.w3.org/2001/XMLSchema-instance"><AddressDetail> <Address> </Address> </AddressDetail></AddressTest>



this is the ouput i want:
<?xml version="1.0" encoding="UTF-8"?>
<AddressTest xmlns="http://www.w3.org/2001/XMLSchema-instance">
      <AddressDetail>
            <Address>101 state st</Address>
      </AddressDetail>
      <AddressDetail>
            <Address>201 exit st</Address>
      </AddressDetail>
      <AddressDetail>
            <Address>301 entrance st</Address>
      </AddressDetail>
</AddressTest>
gr8gonzo

So are you just concerned about the line breaks and indents?
GeneBe

ASKER
Yes line breaks and indents as well as data. i am not getting any data. when i do a debug i see the data in the variable but it does not get to the output
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gr8gonzo

I would just use regular XML serialization instead of doing this manually:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace WpfApp3
{
    public class ExpertsExchange_29178928
    {

        public ExpertsExchange_29178928()
        {
            // Create test data
            var memberSpansTest = new MemberSpansTest();
            memberSpansTest.MemberSpansDetail.Add(new MemberSpansDetail() { Address = "101 state st" });
            memberSpansTest.MemberSpansDetail.Add(new MemberSpansDetail() { Address = "201 exit st" });
            memberSpansTest.MemberSpansDetail.Add(new MemberSpansDetail() { Address = "301 entrance st" });

            // Serialize data to XML
            using (var ms = new MemoryStream())
            using(var sw = new StreamWriter(ms))
            {
                var xs = new XmlSerializer(typeof(MemberSpansTest));
                xs.Serialize(ms, memberSpansTest);
                var xml = Encoding.UTF8.GetString(ms.ToArray());
            }
        }

        // -------------------------------------------------------------------------------------------------

        [Serializable]
        public class MemberSpansTest
        {
            [XmlElement(ElementName = "MemberSpansDetail")]
            public List<MemberSpansDetail> MemberSpansDetail { get; set; } = new List<MemberSpansDetail>();
        }

        [Serializable]
        public class MemberSpansDetail
        {
            public string Address { get; set; }
        }


    }
}

Open in new window


Result:
<?xml version="1.0"?>
<MemberSpansTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MemberSpansDetail>
    <Address>101 state st</Address>
  </MemberSpansDetail>
  <MemberSpansDetail>
    <Address>201 exit st</Address>
  </MemberSpansDetail>
  <MemberSpansDetail>
    <Address>301 entrance st</Address>
  </MemberSpansDetail>
</MemberSpansTest>

Open in new window


I used the XML tags you mentioned in your original question/post (you changed to AddressDetail in another comment, but that's just a matter of changing the XmlElement name).
GeneBe

ASKER
Did you put it in a console application in VS?
ASKER CERTIFIED SOLUTION
gr8gonzo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
GeneBe

ASKER
this was very good! thank you for your quick response and great solution!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy