Hi
I am new to c# and xml but progressing nicely (I think) anyway I am reading in an xml file and then extracting information from a particular tag and rewriting it to a new file. I have almost got it but I am having a problem getting the value from the element and then writing it as an attribute to the new one. Code is
while
(reader.Read()){
if (reader.Name == "text" && reader.NodeType == XmlNodeType.Element)
{
writexml.WriteStartElement("text");
writexml.WriteAttributeString("id", "value from element here");// this bit
writexml.WriteAttributeString(
"x", reader.GetAttribute("x"));writexml.WriteAttributeString(
"y", reader.GetAttribute("y"));writexml.WriteEndElement();
}
sample xml
<rubbish value="I have loads of tags that dont need because they contain rubbish">Rubbish</rubbish>
<text x="300" y="200" colour="blue" font-size="5">Door</text>
which then I get to this stage
<
xml version="1.0" encoding="utf-8" ><textdata><text id="value from element here" x="347.0284" y="543.815" />Problem I have is that I cannot get the value from the original xml >Door</text> into the id
I need to get the value of the current element that its reading and put it in so it's
< xml version="1.0" encoding="utf-8" ><textdata><text id="Door" x="347.0284" y="543.815" />
I could also get away with
< xml version="1.0" encoding="utf-8" ><textdata><text x="347.0284" y="543.815">Door</text>
So close but yet so useless
Regards
Richard

Trouble getting value from xml file
VBScriptor
An XSLT is the best way to do this, but they can be a bit tricky. However, if you want to convert XML from one schema to another, that is the way to go. Otherwise, you'd do better to read your XML into an XmlDataDocument and use Xpath to find the nodes you want to convert, then use them to build a new Xml file.
Roger Jennings
Hi
Thanks guys, I suppose learning c# .net and doing it the right way is also part of the learning.
If you were doing it would you use xslt if yes then I would prefer to learn xslt (will have to at some point) and do it properly. I have already done a few xpath bits so I could use that.
Many thanks
Richard
Jim Carbone
As regards XSLT it also depends on how much information you need and in what format. If you only need the text of one element then it maybe an overkill but as you constructing a new XML file it's an easier way than by using a reader and a writer. The big advantage of your fisrt way is if the source document is very large, then using Xml/XPathDocument may use too much memeory but I would only be worried about that if your document was at least 20Mb.
That does depend in what scenario you're running the code, server, desktop or mobile device for example.
Matt Tompkins
Yes, I would use XSLT. XSLT is a lot more complex than XPath, however. I would commend you if you intend to give it a go, do you have a full version of VS2005 If so, you have an XSLT debugger that will help you enormously.
http://www.mulberrytech.com/quickref/index.html
I recommend printing these out and laminating them :-)
ijprest
It almost certainly doesn't. Stuff like XML Spy and Stylus Studio have XSLT debuggers and 30 day free trials, I'd say that's the way to go.
QuantumArchitect
If you definitely want to use the Xml(Text)Reader then have a look at the ReadElementString() method.
As cgraus pointed out an easier way might be XPathDocument and SelectSingleNode().
Joe
FY
Hi
The original files can be 1mb to 20mb
The file contains a load of rubbish that I dont need so thats why im extracting all the <text> tags and 2 out of the 6 attributes that are in there. There could be 100-200 <text> tags in the final version (what I'm calling the clean version)
The file comes out to about 26k (minus the value of the element that I can't get) after its been cleaned up.
The large file that needs cleaning up will probably be supplied once per month and will overwrite the existing file.
By the way, this is the first forum i've posted to which have got quick and helpful answers and I appreciate this very much
Richard
gatoatigrado
For 20mb input you are on the right track. You were missing one line of code:
using (XmlReader reader = XmlReader.Create(new StringReader(xml))) {using (XmlWriter writexml = XmlWriter.Create(Console.Out)) {
while (reader.Read()) {
if (reader.Name == "text" && reader.NodeType == XmlNodeType.Element) {
writexml.WriteStartElement("text");
writexml.WriteAttributeString("id", "value from element here");
writexml.WriteAttributeString("x", reader.GetAttribute("x"));
writexml.WriteAttributeString("y", reader.GetAttribute("y"));
writexml.WriteString(reader.ReadString());
writexml.WriteEndElement();
}
}
}
}
The using blocks are also important to ensure you close files and release handles in a timely manner rather than waiting on GC to kick in.
spshah
Thanks cgraus
I am using VS Studio 2005 express, will check if it has a debugger, if not then its Google for xslt examples
Many thanks
Richard