DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Wed, 22 Aug 2007 09:32:41 -0400,    posted on: microsoft.public.dotnet.framework        back       

Thread Index
  1    lucius am
          2    (Steven Cheng[MSFT])


Add Namespaces with XmlTextReader and XmltextWriter?   
Framework 1.1, I have an existing complex XML document. I need to add
some namespace information to certain elements. Can anyone illustrate
how to use and XmlTextReader and XmlTextWriter to add custom namespace
info where I want it?

Thanks.
Date:Wed, 22 Aug 2007 09:32:41 -0400   Author:  

RE: Add Namespaces with XmlTextReader and XmltextWriter?   
Hi Lucius,

Nice to see you and how are you doing?

Regarding on the question about add namespace into certain element in XML 
document, here are some of my understanding and suggestion:

** If it possible to use XML Document? If so, you can simply load the xml 
document into memory and add the namespace as an xmlattribute onto the 
certain element. e.g.

=====================
   static void RunDOM()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"..\..\data.xml");



            Console.WriteLine(doc.OuterXml);

            XmlElement rootelm = doc.DocumentElement;

            XmlAttribute attr = doc.CreateAttribute("xmlns", "myns", 
"http://www.w3.org/2000/xmlns/");
            attr.Value = "http://mytest.org/testxml";

            rootelm.Attributes.Append(attr);


            Console.WriteLine("\r\n\r\n\r\n\r\n") ;


            Console.WriteLine(doc.OuterXml);

}

this would be simple and convenient and work well as long as the xml 
document is not quite large.



** For xmlreader/ xmlwriter approach, you can first write out xml nodes 
while reading from XmlReader(in a while loop) and do some 
customziation(such as insert attributes) whenever the xmlreader arrive the 
node you want. Here is a test code snippet demonstrate this:

=========================================

         static void RunReaderWriter()
        {
            XmlTextReader xtr = new XmlTextReader(
                new StreamReader(@"..\..\data.xml", Encoding.UTF8)
            );

            XmlTextWriter xtw = new XmlTextWriter("output1.xml", 
Encoding.UTF8);

            while (xtr.Read())
            {


                if (xtr.IsStartElement() && xtr.NodeType == 
XmlNodeType.Element && xtr.LocalName == "PurchaseOrder")
                {
                    Console.WriteLine("here come the 'PurchaseOrder' 
element.");

                    //add new namespace attr
                    xtw.WriteStartElement(xtr.Prefix, xtr.LocalName, 
xtr.NamespaceURI);
                    xtw.WriteAttributeString(
                       "xmlns", "myns", "http://www.w3.org/2000/xmlns/",
                    "http://mytest.org/testxml"
                    );

                    //copy existing attrs
                    xtw.WriteAttributes(xtr, true);

                }
                else
                {


                    xtw.WriteNode(xtr, true);
                } 
            }

            xtr.Close();
            xtw.Close();
        }

=====================================

and below is the test xml document for your reference
=====================================

<?xml version="1.0" encoding="utf-8" ?>
<PurchaseOrder>
  <Number>1001</Number>
  <OrderDate>8/12/01</OrderDate>
  <BillToAddress>
    <Street>101 Main Street</Street>
    <City>Charlotte</City>
    <State>NC</State>
    <ZipCode>28273</ZipCode>
  </BillToAddress>
  <ShipToAddress>
    <Street>101 Main Street</Street>
    <City>Charlotte</City>
    <State>NC</State>
    <ZipCode>28273</ZipCode>
  </ShipToAddress>
  <LineItem Name="Computer Desk" Description="Wood desk for computer"
  SKU="12345A123" Price="499.99" Qty="1" />
</PurchaseOrder>

============================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

 

==================================================

Get notification to my posts through email? Please refer to 
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

 

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues 
where an initial response from the community or a Microsoft Support 
Engineer within 1 business day is acceptable. Please note that each follow 
up response may take approximately 2 business days as the support 
professional working with you may need further investigation to reach the 
most efficient resolution. The offering is not appropriate for situations 
that require urgent, real-time or phone-based interactions or complex 
project analysis and dump analysis issues. Issues of this nature are best 
handled working with a dedicated Microsoft Support Engineer by contacting 
Microsoft Customer Support Services (CSS) at 
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================
 	

This posting is provided "AS IS" with no warranties, and confers no rights.
Date:Thu, 23 Aug 2007 04:17:04 GMT   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us