tags:

views:

66

answers:

2

hello,I'm a beginner in c#,i want to parse the follows xml use XmlTextReader as a example,then put the content of xml to the stringbuilder,as" Novel+hardcover+1+Margatet+....+1+SqlServer".Which kind and warmhearted man can help me,thanks, i serach http://www.codeproject.com/KB/cs/xml_parsing.aspx , http://www.codeproject.com/info/search.aspx?artkw=XmlTextReader&vidlst=64%2c65%2c69%2c81%2c94&sa_ao=False&sa_so=17&sa_as=1%2c3&aidlst=64%2c65%2c69%2c81 , and google reading xml with XmlTextReader ,the result is not i need,if i have some logic to deal with this problem,i cannot ask somebody to parse it.im having trouble reading the correct nodes. how do i get it to move to the appropriate node

 <?xml version="1.0"?>
   <Bookstore>
    <Book Genre="Novel" Style="hardcover">
     <author id="1">
         <first-name>Margaret</first-name>
         <last-name>Atwood</last-name>
     </author>
     <Title>The Handmaid's Tale</Title>
     <Price>$19.25</Price>
   </Book>
  <GeneralSettings> 
     <RecentID>0</RecentID> 
      <LastUpdate>1967-08-15</LastUpdate> 
      <EnableAutoUpdate>1</EnableAutoUpdate> 
      <ShareSum>0</ShareSum> 
  </GeneralSettings> 
<CodeCatag ID="1" Description="SqlServer"> 
</Bookstore>
A: 

This is a bit fixed but would work

var sb = new StringBuilder();
var root = XElement.Parse(xml);

sb.Append(root.Elements("Book").Attributes("Genre").Value);
sb.Append("+");
sb.Append(root.Elements("Book").Attributes("Style").Value);
sb.Append("+");
sb.Append(root.Elements("Book").Elements("author").Attributes("id").Value);

etc...

Noel Abrahams
i donot know how to parse. thank you answer
pengwang
the XElement.Parse above does the parsing of the xml for you, giving you a structure you can query using xpath or programatically as shown above. Are you wanting to parse the xml yourself?
David
A: 

You could also try LinqToXml. I've put together a quick example which would work for a case where you had multiple books within your bookstore root element:

XDocument doc = XDocument.Load(@"c:\\temp\test.xml");
        var query = from c in doc.Descendants("Book")
                    select c;

        foreach(XElement xe in query)
        {
            Console.WriteLine(xe.Name + "+" + xe.Attribute("Genre").Value + "+" + xe.Attribute("Style").Value);
            Console.WriteLine(xe.Element("author").Attribute("id").Value);
//etc etc etc
        }

Obviously, the Console.WriteLine statements would be replaced with StringBuilder.Append statements in your final solution!

jules
Linq To XML gets my vote: http://msdn.microsoft.com/en-us/library/bb387098.aspx
kyndigs
this can be used in WM? this library looks very vell
pengwang
hi pengwang - what is WM?
jules
windows mobile6 ,because xpath cannot use in WM,XmlTextReader can,so i ask this question
pengwang