Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: How to insert one line after declaration in exsiting xml file?

Reply
Thread Tools

Re: How to insert one line after declaration in exsiting xml file?

 
 
Martin Honnen
Guest
Posts: n/a
 
      11-15-2009
aspfun via DotNetMonster.com wrote:
> Here is partial of existing xml file:
>
> <?xml version="1.0" standalone="yes" ?>
> <NewDataSet>
> <SpecSum>
> <USER>ANDYK</USER>
> <Grouper>Allergy/Immunology</Grouper>
> <Auths>0</Auths>
> <Cost>0</Cost>
> </SpecSum>
> <SpecSum>
>
> In asp.net, how to insert one line after declaration in exsiting xml file?
>
> <?xml version="1.0" standalone="yes" ?>
> <insert new line here.......>
> <NewDataSet>
> <SpecSum>
> <USER>ANDYK</USER>
> <Grouper>Allergy/Immunology</Grouper>
> <Auths>0</Auths>
> <Cost>0</Cost>
> </SpecSum>
> <SpecSum>


The XML object models like DOM (System.Xml.XmlDocument in the .NET
framework) or like LINQ to XML (System.Xml.Linq.XDocument) do not
operate on lines, rather they build a tree model of nodes.
It is not really clear what you want to achieve by the above sample as
simply putting a new line before the root element is not meaningful XML
markup.

Assuming you have

<NewDataSet>
<SpecSum>
<USER>ANDYK</USER>
<Grouper>Allergy/Immunology</Grouper>
<Auths>0</Auths>
<Cost>0</Cost>
</SpecSum>
</NewDataSet>

and want to get a new root element wrapping the old root element you can
do that with LINQ to XML as follows:

XDocument doc = XDocument.Load(MapPath("input.xml");
doc.Root.ReplaceWith(new XElement("New-Root", doc.Root));
doc.Save(MapPath("output.xml"));

For the above sample the result is

<New-Root>
<NewDataSet>
<SpecSum>
<USER>ANDYK</USER>
<Grouper>Allergy/Immunology</Grouper>
<Auths>0</Auths>
<Cost>0</Cost>
</SpecSum>
</NewDataSet>
</New-Root>



--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      11-16-2009
On Nov 16, 3:01*pm, "aspfun via DotNetMonster.com" <u53138@uwe> wrote:
> Martin Honnen wrote:
> >> Here is partial of existing xml file:

>
> >[quoted text clipped - 20 lines]
> >> * </SpecSum>
> >> <SpecSum>

>
> >The XML object models like DOM (System.Xml.XmlDocument in the .NET
> >framework) or like LINQ to XML (System.Xml.Linq.XDocument) do not
> >operate on lines, rather they build a tree model of nodes.
> >It is not really clear what you want to achieve by the above sample as
> >simply putting a new line before the root element is not meaningful XML
> >markup.

>
> >Assuming you have

>
> ><NewDataSet>
> > * <SpecSum>
> > * * <USER>ANDYK</USER>
> > * * <Grouper>Allergy/Immunology</Grouper>
> > * * <Auths>0</Auths>
> > * * <Cost>0</Cost>
> > * </SpecSum>
> ></NewDataSet>

>
> >and want to get a new root element wrapping the old root element you can
> >do that with LINQ to XML as follows:

>
> > * * * * * * XDocument doc = XDocument.Load(MapPath("input..xml");
> > * * * * * * doc.Root.ReplaceWith(new XElement("New-Root", doc.Root));
> > * * * * * * doc.Save(MapPath("output.xml"));

>
> >For the above sample the result is

>
> ><New-Root>
> > * <NewDataSet>
> > * * <SpecSum>
> > * * * <USER>ANDYK</USER>
> > * * * <Grouper>Allergy/Immunology</Grouper>
> > * * * <Auths>0</Auths>
> > * * * <Cost>0</Cost>
> > * * </SpecSum>
> > * </NewDataSet>
> ></New-Root>

>
> Thank you for help. I'll try it later.
> What I need is to code to insert a new line like below so that third party
> program will read this filewihtout this line, third party program will
> return an error:
> -----
> <dataroot xmlnsd="urn:schemas-microsoft-comfficedata" generated="2009-11-
> 13T11:28:52">
> -------
> After all, the xml file will become like belowdate and time will will
> change on every run time)
>
> <?xml version="1.0" standalone="yes" ?>
> <dataroot xmlnsd="urn:schemas-microsoft-comfficedata" generated="2009-11-
> 13T11:28:52">
> *<NewDataSet>
> *<SpecSum>
> * <USER>ANDYK</USER>
> * <Grouper>Allergy/Immunology</Grouper>
> * <Auths>0</Auths>
> * <Cost>0</Cost>
> * </SpecSum>
> <SpecSum>
>
> --
> Message posted via DotNetMonster.comhttp://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net/200911/1- Hide quoted text -
>
> - Show quoted text -


Well, that is not a "new line", moreover, in your example <dataroot>
tag is not closed and looks like a format for MS Access, that means
there is another "line" you would need to "insert".

So, if you have already a document, you need to create a new one, add
<dataroot> as a root element and copy all nodes from the source
document.

Here's an example:

System.Xml.XmlDocument src = new System.Xml.XmlDocument();

src.LoadXml(@"<?xml version=""1.0"" standalone=""yes"" ?>
<NewDataSet>
<SpecSum>
<USER>ANDYK</USER>
<Grouper>Allergy/Immunology</Grouper>
<Auths>0</Auths>
<Cost>0</Cost>
</SpecSum>
</NewDataSet>
");


XmlDocument doc = new XmlDocument();
XmlNode declaration = doc.CreateXmlDeclaration("1.0", "", "yes");
doc.AppendChild(declaration);

XmlElement root = doc.CreateElement("dataroot");
doc.AppendChild(root);

XmlAttribute od = doc.CreateAttribute("xmlnsd");
od.Value = "urn:schemas-microsoft-comfficedata";
root.Attributes.Append(od);

XmlAttribute generated = doc.CreateAttribute("generated");
generated.Value = System.Xml.XmlConvert.ToString(DateTime.Now,"yyyy-MM-
ddTHH:mm:ss");
root.Attributes.Append(generated);

foreach (XmlNode node in src.SelectNodes("NewDataSet"))
{
root.AppendChild(doc.ImportNode(node, true));
}

doc.Save(MapPath("new.xml"));

Let me know if you have any questions
 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      11-17-2009
On Nov 16, 6:30*pm, "aspfun via DotNetMonster.com" <u53138@uwe> wrote:
> aspfun wrote:
> >>> >> Here is partial of existing xml file:

>
> >[quoted text clipped - 49 lines]

>
> >>Let me know if you have any questions

>
> >Hi, Alexey Smirnov,
> >I tried you code and load a real xml file but got an error at loading src.
> >The new root is perfect.

>
> >Dim doc As New XmlDocument()
> > * * * *Dim declaration As XmlNode = doc.CreateXmlDeclaration("1.0", "",
> >"yes")
> > * * * *doc.AppendChild(declaration)

>
> > * * * *Dim root As XmlElement = doc.CreateElement("dataroot")
> > * * * *doc.AppendChild(root)

>
> > * * * *Dim od As XmlAttribute = doc.CreateAttribute("xmlnsd")
> > * * * *od.Value = "urn:schemas-microsoft-comfficedata"
> > * * * *root.Attributes.Append(od)

>
> > * * * *Dim generated As XmlAttribute = doc.CreateAttribute("generated")
> > * * * *generated.Value = System.Xml.XmlConvert.ToString(DateTime.Now, "yyyy-
> >MM- ddTHH:mm:ss")
> > * * * *root.Attributes.Append(generated)

>
> > //here is error: Data at the root level is invalid. Line 1, position 1

>
> >Dim src As New XmlDocument
> > * * * *src.LoadXml(MapPath("Order.xml"))

>
> > * * * *For Each node As XmlNode In src.SelectNodes("NewDataSet")
> > * * * * * *root.AppendChild(doc.ImportNode(node, True))
> > * * * *Next node

>
> > * * * *doc.Save(MapPath("new.xml"))

>
> Good news!
> Once I changed
> from
> src.LoadXml(MapPath("Order.xml"))
> to
> src.Load(MapPath("Order.xml"))
> It works great!
> Thank all of you!
>
> --
> Message posted via DotNetMonster.comhttp://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net/200911/1- Hide quoted text -
>
> - Show quoted text -


LoadXml Method loads the XML document from the specified string, and
Load Method loads it from the file. Great that it works now.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Insert Multiple Records Using One Insert Statemen with MySQLdb module anton.ranieri.it@gmail.com Python 1 12-06-2008 12:47 PM
Add to exsiting value on update Mangler ASP General 1 10-23-2006 03:57 PM
Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file jkflens XML 2 05-30-2006 09:41 AM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM



Advertisments