Xml root element attributes

M

Mikus Sleiners

I want to create xml document that looks like this:

<?xml version="1.0" encoding="utf-16"?>
<Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="TravelDraft">
<DraftId>139451de-1bc1-4070-b9d1-548dd4a7f812</DraftId>
<StartDate>2006-10-04T00:00:00</StartDate>
<RetryCount>0</RetryCount>
<LocalCurrencyId>1</LocalCurrencyId>
<EndDate>2006-10-25T23:59:59</EndDate>
<OriginalPremium>38.61</OriginalPremium>
<Locale>lt-LT</Locale>
<IssueDate>2006-10-02T09:26:31.8562127+03:00</IssueDate>
<StatusCode>Unsuccessful</StatusCode>
<Invoice>
<Number>B0002624</Number>
<IssueDate>2006-10-02T09:26:31.8562127+03:00</IssueDate>
<DueDate>2006-10-02T09:26:31.8562127+03:00</DueDate>
</Invoice>
<PolicyNumber>45300000265</PolicyNumber>
<PolicyHolderName>AS AS, 123456-12345</PolicyHolderName>
<OriginalCurrencyId>3</OriginalCurrencyId>
<TransactionId>FVR21tY1Kot1fOfFRqkFJ9WZ7E4=</TransactionId>
<LocalPremium>27.03</LocalPremium>
<PaymentType>Ita</PaymentType>
<MediatorName>Air Baltic Corporation AS</MediatorName>
<LobId>53</LobId>
<PolicyHolder>
<Name>AS</Name>
<Surname>AS</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>false</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Address></Address>
<Phone>11212121</Phone>
<Email>[email protected]</Email>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<Village />
<House>NAMAS</House>
<Flat />
<TownDistrictOid>0</TownDistrictOid>
<RegionCityOid>27</RegionCityOid>
<Street />
<PostalIndex>1212</PostalIndex>
<LegalPersonType>0</LegalPersonType>
</PolicyHolder>
<Country>Estonia</Country>
<Zone>1</Zone>
<TravelType>21</TravelType>
<Program>1</Program>
<InsuredPersons>
<Person>
<Name>AS</Name>
<Surname>AS</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>true</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<LegalPersonType>0</LegalPersonType>
</Person>
<Person>
<Name>AS2</Name>
<Surname>AS2</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>true</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<LegalPersonType>0</LegalPersonType>
</Person>
</InsuredPersons>
</Draft>

As you can see there are 3 attributes on the root level. I am trying to set
these attributes this way:

XmlDocument xml = new XmlDocument();

XmlDeclaration declaration = xml.CreateXmlDeclaration ("1.0",
"utf-16", null);

xml.AppendChild(declaration);

//Root element
XmlElement elemDraft = xml.CreateElement
("Draft");//,"http://www.w3.org/2001/XMLSchema");

//Root element attributes
XmlAttribute attr1 = xml.CreateAttribute ("xmlns:xsi");
attr1.Value = "http://www.w3.org/2001/XMLSchema-instance";
elemDraft.Attributes.Append (attr1);

XmlAttribute attr2 = xml.CreateAttribute ("xmlns:xsd");
attr2.Value = "http://www.w3.org/2001/XMLSchema";
elemDraft.Attributes.Append (attr2);

XmlAttribute attr3 = xml.CreateAttribute ("xsi:type");
attr3.Value = "TravelDraft";
elemDraft.Attributes.Append (attr3);

xml.AppendChild (elemDraft);

when this is done i get following xml document

<?xml version="1.0" encoding="utf-16"?>
<Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="TravelDraft">
</Draft>

Notice that everything is just like i wanted except for last attribute -
type. It should look like xsi:type="TravelDraft", but xsi prefix is absent.
Why is that ?
 
M

Martin Honnen

Mikus said:
I want to create xml document that looks like this:

<?xml version="1.0" encoding="utf-16"?>
<Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="TravelDraft">

Here is one way:

const string xmlnsURI = @"http://www.w3.org/2000/xmlns/";

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0",
"UTF-16", null));

xmlDocument.AppendChild(xmlDocument.CreateElement("Draft"));

XmlAttribute xmlnsXsi = xmlDocument.CreateAttribute("xmlns",
"xsi", xmlnsURI);
xmlnsXsi.Value = XmlSchema.InstanceNamespace;
xmlDocument.DocumentElement.SetAttributeNode(xmlnsXsi);

XmlAttribute xmlnsXsd = xmlDocument.CreateAttribute("xmlns",
"xsd", xmlnsURI);
xmlnsXsd.Value = XmlSchema.Namespace;
xmlDocument.DocumentElement.SetAttributeNode(xmlnsXsd);

XmlAttribute xsiType = xmlDocument.CreateAttribute("xsi", "type",
XmlSchema.InstanceNamespace);
xsiType.Value = "TravelDraft";
xmlDocument.DocumentElement.SetAttributeNode(xsiType);

You need
using System.Xml;
using System.Xml.Schema;
for that code.
 
M

Mikus Sleiners

Thanks Martin.


Martin Honnen said:
Here is one way:

const string xmlnsURI = @"http://www.w3.org/2000/xmlns/";

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0",
"UTF-16", null));

xmlDocument.AppendChild(xmlDocument.CreateElement("Draft"));

XmlAttribute xmlnsXsi = xmlDocument.CreateAttribute("xmlns", "xsi",
xmlnsURI);
xmlnsXsi.Value = XmlSchema.InstanceNamespace;
xmlDocument.DocumentElement.SetAttributeNode(xmlnsXsi);

XmlAttribute xmlnsXsd = xmlDocument.CreateAttribute("xmlns", "xsd",
xmlnsURI);
xmlnsXsd.Value = XmlSchema.Namespace;
xmlDocument.DocumentElement.SetAttributeNode(xmlnsXsd);

XmlAttribute xsiType = xmlDocument.CreateAttribute("xsi", "type",
XmlSchema.InstanceNamespace);
xsiType.Value = "TravelDraft";
xmlDocument.DocumentElement.SetAttributeNode(xsiType);

You need
using System.Xml;
using System.Xml.Schema;
for that code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top