473,504 Members | 13,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Xml root element attributes

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
2 14184
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2687
by: Wolfgang | last post by:
I'm applying a simple XSLT style sheet to an XML file. The style sheet is: http://piru.alexandria.ucsb.edu/~rnott/MetadataMapping/access-report.xsl The XML file is here: ...
1
1818
by: Wolfgang | last post by:
XSLT transformations by default seem to pass name space attributes into the root element of their output (example below). QUESTION: Is it possible to control this, i.e. not genrating a name...
6
8319
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get...
16
9490
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
2
2103
by: fizzy | last post by:
i am fetching an xml document with the following structure: <?xml version="1.0" encoding="UTF-8"?> <DTCResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
9
6607
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
1
1676
by: hilz | last post by:
Hi. I have two xml files with the following roots: <MyRootElement xmlns="http://myURI" xmlns:prefixA="http://URI_for_A" xmlns:prefixB="http://URI_for_B"...
16
3496
by: TT (Tom Tempelaere) | last post by:
Hi all, I created an XSD to define the structure of an XML file for my project. I made an XML file linked to the XSD using XmlSpy. The problem is that if I read the file using .NET XmlDocument...
0
964
by: gusgar | last post by:
How do I program to create a root element with two attributes or an element with two attributes? I am using VB6 and dom and xml3.0. Example: <frio tiempo="cold" per="5"> Root or node frio has...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.