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

Discussion in 'ASP .Net' started by Martin Honnen, Nov 15, 2009.

  1. 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/
     
    Martin Honnen, Nov 15, 2009
    #1
    1. Advertising

  2. 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 file:(wihtout this line, third party program will
    > return an error:
    > -----
    > <dataroot xmlns:eek:d="urn:schemas-microsoft-com:eek:fficedata" generated="2009-11-
    > 13T11:28:52">
    > -------
    > After all, the xml file will become like below:(date and time will will
    > change on every run time)
    >
    > <?xml version="1.0" standalone="yes" ?>
    > <dataroot xmlns:eek:d="urn:schemas-microsoft-com:eek:fficedata" 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("xmlns:eek:d");
    od.Value = "urn:schemas-microsoft-com:eek:fficedata";
    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
     
    Alexey Smirnov, Nov 16, 2009
    #2
    1. Advertising

  3. 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("xmlns:eek:d")
    > >        od.Value = "urn:schemas-microsoft-com:eek:fficedata"
    > >        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.
     
    Alexey Smirnov, Nov 17, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.frontiernet.net
    Replies:
    6
    Views:
    1,152
    news.frontiernet.net
    Apr 16, 2004
  2. loveNUNO
    Replies:
    2
    Views:
    955
    loveNUNO
    Nov 20, 2003
  3. jkflens
    Replies:
    2
    Views:
    1,534
    jkflens
    May 30, 2006
  4. scad
    Replies:
    23
    Views:
    1,204
    Alf P. Steinbach
    May 17, 2009
  5. Mangler

    Add to exsiting value on update

    Mangler, Oct 23, 2006, in forum: ASP General
    Replies:
    1
    Views:
    130
    Mike Brind
    Oct 23, 2006
Loading...

Share This Page