这里要读一个xml文件,并把数据写入csv。 python代码 python 3.8 def read_xml3 ( ) : doc = etree . parse ( "D:/tmp/bbc_text/bbc-text.xml" ) root = doc . getroot ( ) news_items = [ ] with open ( "D:/tmp/bbc_text/bbc_text_python.csv" , "w" , encoding = "utf-8" ) as f : f . write ( "category,text\n" ) for news_item_xml in root . getchildren ( ) : f . write ( news_item_xml . attrib [ 'category' ] ) f . write ( "," ) f . write ( news_item_xml . text . replace ( '"' , '""' ) ) f . write ( "\n" ) f . flush ( ) f . close ( ) C#代码 public class BbcNewsItem { public string Category { get ; set ; } public string Text { get ;