Last post Oct 17, 2013 11:56 AM by tutumon79
Member
9 Points
97 Posts
Oct 15, 2013 12:52 PM|tutumon79|LINK
HI I am trying to generate an XML document like this through code. <?xml version="1.0" encoding="windows-1252" ?> <TestRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://localhost:2292/RMSchema.xsd"> <Version>3</Version> <ApplicationHeader> <AppLanguage></AppLanguage> <UserId>rmservice</UserId> </ApplicationHeader> <CustomerData> <ExistingCustomerData> <MTN>2084127182</MTN> </ExistingCustomerData> </CustomerData> </TestRequest> I tried some samples. But they create xmlns for the children too, which i dont need. Any help is really appreciated. Thanks Tutu
asp.net
All-Star
15648 Points
2151 Posts
Oct 16, 2013 04:48 AM|Happy Chen - MSFT|LINK
hi tutumon79,
I would suggest you try the follow workaround:
XmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); XmlDocument.DocumentElement.SetAttribute("xmlns:xsd", "http://localhost:2292/RMSchema.xsd");
Please read the reference below for more information:
http://stackoverflow.com/questions/2920142/how-to-add-xmlnamespace-to-a-xmldocument
http://social.msdn.microsoft.com/Forums/vstudio/en-us/786bad59-c9d2-4dd0-8941-b746f253dcef/how-to-create-xml-document-in-cnet?forum=csharpgeneral
i hope it helps you.
Oct 17, 2013 11:56 AM|tutumon79|LINK
I solved it like this
XmlDocument xDocument = new XmlDocument();
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null)); var xsi = "http://www.w3.org/2001/XMLSchema-instance"; XmlElement xRoot = xDocument.CreateElement("TestRequest"); var attr = xDocument.CreateAttribute("xmlns:xsi"); attr.Value = xsi; xRoot.Attributes.Append(attr); attr = xDocument.CreateAttribute("noNamespaceSchemaLocation", xsi); attr.Value = "http://localhost:2292/TestSchema.xsd"; xRoot.Attributes.Append(attr); xDocument.AppendChild(xRoot);
Member
9 Points
97 Posts
Create XMLDocument through Code
Oct 15, 2013 12:52 PM|tutumon79|LINK
HI
I am trying to generate an XML document like this through code.
<?xml version="1.0" encoding="windows-1252" ?>
<TestRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://localhost:2292/RMSchema.xsd">
<Version>3</Version>
<ApplicationHeader>
<AppLanguage></AppLanguage>
<UserId>rmservice</UserId>
</ApplicationHeader>
<CustomerData>
<ExistingCustomerData>
<MTN>2084127182</MTN>
</ExistingCustomerData>
</CustomerData>
</TestRequest>
I tried some samples. But they create xmlns for the children too, which i dont need. Any help is really appreciated.
Thanks
Tutu
asp.net
All-Star
15648 Points
2151 Posts
Re: Create XMLDocument through Code
Oct 16, 2013 04:48 AM|Happy Chen - MSFT|LINK
hi tutumon79,
I would suggest you try the follow workaround:
Please read the reference below for more information:
http://stackoverflow.com/questions/2920142/how-to-add-xmlnamespace-to-a-xmldocument
http://social.msdn.microsoft.com/Forums/vstudio/en-us/786bad59-c9d2-4dd0-8941-b746f253dcef/how-to-create-xml-document-in-cnet?forum=csharpgeneral
i hope it helps you.
asp.net
Member
9 Points
97 Posts
Re: Create XMLDocument through Code
Oct 17, 2013 11:56 AM|tutumon79|LINK
I solved it like this
XmlDocument xDocument = new XmlDocument();
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null));
var xsi = "http://www.w3.org/2001/XMLSchema-instance";
XmlElement xRoot = xDocument.CreateElement("TestRequest");
var attr = xDocument.CreateAttribute("xmlns:xsi");
attr.Value = xsi;
xRoot.Attributes.Append(attr);
attr = xDocument.CreateAttribute("noNamespaceSchemaLocation", xsi);
attr.Value = "http://localhost:2292/TestSchema.xsd";
xRoot.Attributes.Append(attr);
xDocument.AppendChild(xRoot);
asp.net