< message id=\"1\" name=\"last\" text=\"welcome All\"/>" />
开发者

adding Lines to Xml file At Specific location

开发者 https://www.devze.com 2023-02-01 05:12 出处:网络
I have an xml file \"message.xml\" in which messages are written like :- < messages> < message id=\"1\" name=\"last\" text=\"welcome All\"/>

I have an xml file "message.xml" in which messages are written like :-

< messages>

< message id="1" name="last" text="welcome All"/>

-..

-..

-..

< message id="10" name="first" text="welcome"/>

< /message>

Now i have开发者_开发问答 to add message lines after last message each time through my program,

the problem is how to find the last line or place where i have to add the lines (like just before< /message> tag) ??


You could use a XDocument to manipulate XML files:

XDocument
    .Load("test.xml")
    .Root
    .Add(
        new XElement(
            "message", 
            new XAttribute("id", "123"),
            new XAttribute("name", "foo"),
            new XAttribute("text", "bar")
        )
    )
    .Save("test.xml");


Load the file into an XmlDocument (or an XDocumnent). Then you can add new message elements to the root messages element. Then resave it. No need to do text-file editing.

0

精彩评论

暂无评论...
验证码 换一张
取 消