开发者

streamreader to read out lines of xml file

i would like to read out an xml file into a textblock and users can edit this text in the textblock and apply the append changes back to the xml file... i am currently stuck. This is what i've done so far:

    private void Editbuildstreams_Click(object sender, RoutedEventArgs e)
    {
        BuildstreamTextblock.Visibility = Visibility.Visible;
        using (StreamReader srr = new StreamReader("initial.xml"))
        {
        开发者_Python百科    string line;
            while ((line = srr.ReadLine()) != null)
            {
                BuildstreamTextblock.Text = line;
            }
        }
    }

here is the structure of the xml file:

<xml>
   <email>
..
   </email>
   <buildstream1>
      <path value="apple"/>
   </buildstream1>
   <buildstream2>
      <path value="pear"/>
      <path value="bananas"/>
   </buildstream2>
</xml>

the question are:

how do i read out the lines of the xml file?

i am only interested in obtaining certain parts of the xml file.how do i only get <buildstream1> & <buildstream2> to be read out?


Something like this?

var originalDocument = XDocument.Load(fileName);
var originalElement = originalDocument.XPathSelectElement("xml/buildstream1");
textBox.Text = originalElement.ToString();

// do changes in the text box

var newDocument = XDocument.Parse(textBox.Text);
var newElement = newDocument.Elements().Single();

// insert edited element
originalElement.AddAfterSelf(newElement);

// remove original element
originalElement.Remove();
originalDocument.Save(fileName);

// or

var resultXml = originalDocument.ToString();


I'd recommend not trying to read it like a text file and instead use a data adapter. As long as the xml file is valid xml it works quite well.

You could then use a databound textbox, datagrid, combobox or various other tools depending on the options needed - further you wouldn't have to worry about users messing up the structure of the xml or writing your own logic to filter.

Here is some code samples that use roughly the same approach I'd take. Googling will reveal hundres more.

http://www.codeproject.com/KB/cpp/dataset.aspx


you might want to look at XML Parsing and Easy XML Parsing

Hope this Helps


Have a look at Using XPathReader (this is a good article that, no doubt, you will want to read fully). Example:

<xml>
   <email>
..
   </email>
   <buildstream1>
      <path value="apple"/>
   </buildstream1>
   <buildstream2>
      <path value="pear"/>
      <path value="bananas"/>
   </buildstream2>
</xml>

example

using System; 
using System.Xml;
using System.Xml.XPath;
using GotDotNet.XPath;

public class Test
{
    public static void Main(string[] args)
    {
        try
        {
            XPathReader xpr  = new XPathReader("input.xml", "//*/path@value");

            while (xpr.ReadUntilMatch())
                Console.WriteLine(xpr.ReadString());
            Console.ReadLine();
        }
        catch(XPathReaderException xpre)
        { Console.WriteLine("XPath Error: " + xpre); }
        catch(XmlException xe)
        { Console.WriteLine("XML Parsing Error: " + xe); }
        catch(IOException ioe)
        { Console.WriteLine("File I/O Error: " + ioe); }
    }
}

Will print

apple
pear
bananas

Google for one of the many XPath tutorials


One way to do this is to read the string and load into XmlDcument. This way you can easily find any specific elements easily.

private void Editbuildstreams_Click(object sender, RoutedEventArgs e)
{
    BuildstreamTextblock.Visibility = Visibility.Visible;
    XmlDocument doc = new XmlDocument();
    using (XmlTextReader reader = new XmlTextReader("initial.xml"))
    {
       reader.Read(); 
       doc.Load(reader);

       StringBuilder line = new StringBuilder();
       line.apend (doc.doc.GetElementsByTagName("buildstream1")[0].outerXml;

       BuildstreamTextblock.Text = line;
    }
}

similarly after manipulating them, you can assign changes back to original xml again by using XmlDocument object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜