By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,783 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,783 IT Pros & Developers. It's quick & easy.

Xml root element attributes

P: n/a
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>AS**@ASAS.LV</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 ?
Jan 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mikus Sleiners wrote:
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.CreateXmlDecla ration("1.0",
"UTF-16", null));

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

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

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

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

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

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 26 '07 #2

P: n/a
Thanks Martin.
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:Oj**************@TK2MSFTNGP05.phx.gbl...
Mikus Sleiners wrote:
>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.CreateXmlDecla ration("1.0",
"UTF-16", null));

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

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

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

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

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

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jan 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.