如何获取XML标记的内部文本并将内部文本保存在文本文件中 [英] How do I get the XML tag's inner text and save the inner text in text file

查看:49
本文介绍了如何获取XML标记的内部文本并将内部文本保存在文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 OpenFileDialog openFileDialog =  new  OpenFileDialog(); 
openFileDialog.Multiselect = true ;
openFileDialog.Filter = 文本文件(* .txt)| * .txt |所有文件(*。 。*)| *。*;
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog( this )== DialogResult.OK)
{
string strFilename = openFileDialog.FileName;
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(strFilename))
{
xmlDoc.Load(strFilename);
string xmlFile = File.ReadAllText(strFilename);
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlFile);
XmlNodeList nodeList = xmldoc.GetElementsByTagName( coordinates);
string coordinates = string .Empty;
foreach
nodeList中的XmlNode节点
{
coordinates = node.InnerText;
string [] lst = coordinates.Split( .ToCharArray());
foreach string str in lst)
{
coordinates + = str + Environment.NewLine;
}
}

SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = 文本文件(* .txt)| * .txt |所有文件(*。 。*)| *。*;
if (savefile.ShowDialog()== DialogResult.OK)
{
使用(StreamWriter sw = new StreamWriter(savefile.FileName))
{
sw.WriteLine(coordinates);
sw.Flush();
}
}
}
}





从解决方案中添加XML 1

输入XML:

 <?  xml     version   =   1.0    encoding   =  UTF-8  < span class =code-summarycomment>>  
< kml xmlns = http://www.opengis.net/kml/2.2 xmlns:gx = http://www.google.com/kml/ext/ 2.2 xmlns:kml = http://www.opengis.net/kml/2.2 xmlns:atom = http://www.w3.org/2005/ Atom >
< 文档 >
< name > Untitled Path.kmz < / name >
< ST yle id = s_ylw-pushpin_hl >
< IconStyle >
< ; scale > 1.3 < / scale >
< ; 图标 >
< href > http://maps.google.com/mapfiles/kml /pushpin/ylw-pushpin.png< / href < span class =code-keyword>>
< / Icon >
< hotSpot x = 20 y = 2 xunits = pixels < span class =code-attribute> yunits = pixels / >
< / IconStyle >
< / Style >
< < span class =code-leadattribute> StyleMap id = m_ylw-pushpin >
< 配对 >
< key > 普通< / key >
< styleUrl > #s_ylw-pushpin < / styleUrl >
< / Pair >
< 配对 >
< key > 突出显示< / key >
< styleUrl > #s_ylw-pushpin_hl < / styleUrl >
< / Pair >
< / StyleMap >
< 样式 id = s_ylw-pushpin >
< IconStyle >
< scale > 1.1 < / scale >
< 图标 >
< href > http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png < / href >
< /图标 >
< hotSpot x = 20 y = 2 xunits = pixels yunits = pixels / >
< / IconStyle >
< / Style >
< 地标 >
< name > Untitled Path < / name >
< styleUrl > #m_ylw-pushpin < / styleUrl >
< LineString >
< tessellate > ; 1 < / tessellate >
< 坐标 > ;
80.4302618226675,14.10146821639644,0 80.42576847769909,14.01560482206596,0 80.42217788625582,13.94694674204649,0 80.41768407455187,13.86119299421783,0 80.39658103437603,13.79361789706296,0 80.39388165361999,13.7422419928809,0 80.390282415801,13.67377041492268,0 80.38305143626342,13.53699640691199,0 80.37760577599572 ,13.43454151969623,0 80.37397453710747,13.36627494143451,0 80.36943988618303,13.28097184062539,0 80.34836784687947,13.21373716074971,0 80.32456264262412,13.09546343074962,0 80.31820720889201,12.97627719905313,0 80.31364934840515,12.89121440605016,0 80.30817653247001,12.78918641302588,0 80.30360336227979,12.70421375659957,0 80.29902651922799,12.61927452126594,0 80.29443669587334 ,12.53437749827671,0 80.28892262385639,12.43254149390298,0 80.28616076426671,12.38164079913633,0 80.26511197822781,12.31474736051312,0 80.26050279541035,12.22995820537765,0 80.23941074889372,12.16313653744558,0 80.21645688792,12.06243774013701,0 80.17619514615069,11.96267868889682,0 80.15607602451276,11.91279853066779,0 80.11770356329768,11.84691871805963,0 80.07750416467992 ,11.74715799351077,0 80.02187143303681,11.6821903960103,0 79.96351572639186,11.56640475126538,0 79.94065777203802,11.4657303741187,0 79.93510632394116,11.36414102440262,0 79.92860153755805,11.24562697520951,0 79.92393943340687,11.1609745136211 3,0 79.91740170713379,11.04245799454426,0 79.91646313955498,11.02552624900637,0
< / coordinates < span class =code-keyword>>

< / LineString >
< / Placemark >
< /文档 >
< / kml >





什么我试过了:



此代码获得的o / p是

80.4302618226675,14.10146821639644,0 80.42576847769909,14.01560482206596, 0 80.42217788625582,13.94694674204649,0 80.41768407455187,13 .86119299421783,0 80.39658103437603,13.79361789706296,0 80.39388165361999,13.7422419928809,0 80.390282415801,13.67377041492268,0 80.38305143626342,13.53699640691199,0 80.37760577599572,13.43454151969623,0 80.37397453710747,13.36627494143451,0 80.36943988618303,13.28097184062539,0

80.4302618226675,14.10146821639644 ,0

80.42576847769909,14.01560482206596,0

80.42217788625582,13.94694674204649,0

80.41768407455187,13.86119299421783,0

80.39658103437603 ,13.79361789706296,0

80.390282415801,13.67377041492268,0

80.38305143626342,13.53699640691199,0

80.37760577599572,13.43454151969623,0

80.37397453710747,13.36627494143451,0

80.36943988618303,13.28097184062539,0



你可以看到这里我真正需要得到的是这个:



80.4302618226675,14.10146821639644,0

80.42576847769909, 14.01560482206596,0
80.42217788625582,13.94694674204649,0

80.41768407455187,13.86119299421783,0

80.39658103437603,13.79361789706296,0

80.39388165361999,13.7422419928809,0

80.390282415801,13.67377041492268,0

80.38305143626342,13.53699640691199,0

80.37760577599572,13.43454151969623,0

80.37397453710747,13.36627494143451,0

80.36943988618303,13.28097184062539,0



但在此之前我不需要以水平方式获得最高价值,有人可以帮我纠正代码以这种方式获取o / p请

o / p:

80.4302618226675,14.10146821639644,0

80.42576847769909,14.01560482206596,0

80.42217788625582,13.94694674204649,0

80.41768407455187,13.86119299421783,0

80.39658103437603,13.79361789706296,0

解决方案

这是一种方法



 XNamespace xns =   http://www.opengis.net/kml/2.2; 
XDocument xdInput = XDocument.Load( @ C:\Temp \ input.xml);
XElement xeDocument = xdInput.Root.Element(xns + Document);
XElement xePlaceMark = xeDocument.Element(xns + Placemark);

XElement xeCoord = xePlaceMark.Descendants(xns + coordinates)。第一();

string [] lines = xeCoord.Value.Trim()。Split(' ');
File.WriteAllLines( @ C:\Temp\Output.txt,线路);





输出:

<前郎=文字> 80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801, 13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0
80.34836784687947,13.21373716074971,0
80.32456264262412,13.09546343074962,0
80.31820720889201,12.97627719905313,0
80.31364934840515,12.89121440605016,0
80.30817653247001,12.78918641302588,0
80.30360336227979,12.70421375659957,0
80.29902651922799,12.61927452126594 ,0
80.29443669587334,12.534377498276 71,0
80.28892262385639,12.43254149390298,0
80.28616076426671,12.38164079913633,0
80.26511197822781,12.31474736051312,0
80.26050279541035,12.22995820537765,0
80.23941074889372,12.16313653744558,0
80.21645688792,12.06243774013701,0
80.17619514615069,11.96267868889682,0
80.15607602451276,11.91279853066779,0
80.11770356329768,11.84691871805963,0
80.07750416467992,11.74715799351077,0
80.02187143303681,11.6821903960103 ,0
79.96351572639186,11.56640475126538,0
79.94065777203802,11.4657303741187,0
79.93510632394116,11.36414102440262,0
79.92860153755805,11.24562697520951,0
79.92393943340687,11.16097451362113,0
79.91740170713379,11.04245799454426,0
79.91646313955498,11.02552624900637,0


OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
    string strFilename = openFileDialog.FileName;
    XmlDocument xmlDoc = new XmlDocument();
    if (File.Exists(strFilename))
    {
        xmlDoc.Load(strFilename);
        string xmlFile = File.ReadAllText(strFilename);
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(xmlFile);
        XmlNodeList nodeList = xmldoc.GetElementsByTagName("coordinates");
        string coordinates = string.Empty;
        foreach (XmlNode node in nodeList)
        {   
            coordinates = node.InnerText;
            string[] lst = coordinates.Split(" ".ToCharArray());
            foreach (string str in lst)
            {
                coordinates += str +  Environment.NewLine;
            }
        }

        SaveFileDialog savefile = new SaveFileDialog();
        savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
        if (savefile.ShowDialog() == DialogResult.OK)
        {
            using (StreamWriter sw = new StreamWriter(savefile.FileName))
            {
                sw.WriteLine(coordinates);
                sw.Flush();
            }
        }
    }
}



[EDIT] Added XML from Solution 1
Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
	<name>Untitled Path.kmz</name>
	<Style id="s_ylw-pushpin_hl">
		<IconStyle>
			<scale>1.3</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
			</Icon>
			<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
		</IconStyle>
	</Style>
	<StyleMap id="m_ylw-pushpin">
		<Pair>
			<key>normal</key>
			<styleUrl>#s_ylw-pushpin</styleUrl>
		</Pair>
		<Pair>
			<key>highlight</key>
			<styleUrl>#s_ylw-pushpin_hl</styleUrl>
		</Pair>
	</StyleMap>
	<Style id="s_ylw-pushpin">
		<IconStyle>
			<scale>1.1</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
			</Icon>
			<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
		</IconStyle>
	</Style>
	<Placemark>
		<name>Untitled Path</name>
		<styleUrl>#m_ylw-pushpin</styleUrl>
		<LineString>
			<tessellate>1</tessellate>
			<coordinates>
				80.4302618226675,14.10146821639644,0 80.42576847769909,14.01560482206596,0 80.42217788625582,13.94694674204649,0 80.41768407455187,13.86119299421783,0 80.39658103437603,13.79361789706296,0 80.39388165361999,13.7422419928809,0 80.390282415801,13.67377041492268,0 80.38305143626342,13.53699640691199,0 80.37760577599572,13.43454151969623,0 80.37397453710747,13.36627494143451,0 80.36943988618303,13.28097184062539,0 80.34836784687947,13.21373716074971,0 80.32456264262412,13.09546343074962,0 80.31820720889201,12.97627719905313,0 80.31364934840515,12.89121440605016,0 80.30817653247001,12.78918641302588,0 80.30360336227979,12.70421375659957,0 80.29902651922799,12.61927452126594,0 80.29443669587334,12.53437749827671,0 80.28892262385639,12.43254149390298,0 80.28616076426671,12.38164079913633,0 80.26511197822781,12.31474736051312,0 80.26050279541035,12.22995820537765,0 80.23941074889372,12.16313653744558,0 80.21645688792,12.06243774013701,0 80.17619514615069,11.96267868889682,0 80.15607602451276,11.91279853066779,0 80.11770356329768,11.84691871805963,0 80.07750416467992,11.74715799351077,0 80.02187143303681,11.6821903960103,0 79.96351572639186,11.56640475126538,0 79.94065777203802,11.4657303741187,0 79.93510632394116,11.36414102440262,0 79.92860153755805,11.24562697520951,0 79.92393943340687,11.16097451362113,0 79.91740170713379,11.04245799454426,0 79.91646313955498,11.02552624900637,0 
			</coordinates>
		</LineString>
	</Placemark>
</Document>
</kml>



What I have tried:

the o/p obtained for this code is
80.4302618226675,14.10146821639644,0 80.42576847769909,14.01560482206596,0 80.42217788625582,13.94694674204649,0 80.41768407455187,13.86119299421783,0 80.39658103437603,13.79361789706296,0 80.39388165361999,13.7422419928809,0 80.390282415801,13.67377041492268,0 80.38305143626342,13.53699640691199,0 80.37760577599572,13.43454151969623,0 80.37397453710747,13.36627494143451,0 80.36943988618303,13.28097184062539,0
80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801,13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0

you can see here o/p which i actually need to get is this:

80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801,13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0

but before that i dont need the top most value in horizantal manner,can someone help me to correct the code to obtain me the o/p in this manner please
o/p:
80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0

解决方案

This is one way to do it

XNamespace xns = "http://www.opengis.net/kml/2.2";
XDocument xdInput = XDocument.Load(@"C:\Temp\input.xml");
XElement xeDocument = xdInput.Root.Element(xns + "Document");
XElement xePlaceMark = xeDocument.Element(xns + "Placemark");

XElement xeCoord = xePlaceMark.Descendants(xns + "coordinates").First();

string[] lines = xeCoord.Value.Trim().Split(' ');
File.WriteAllLines(@"C:\Temp\Output.txt", lines);



Output:

80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801,13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0
80.34836784687947,13.21373716074971,0
80.32456264262412,13.09546343074962,0
80.31820720889201,12.97627719905313,0
80.31364934840515,12.89121440605016,0
80.30817653247001,12.78918641302588,0
80.30360336227979,12.70421375659957,0
80.29902651922799,12.61927452126594,0
80.29443669587334,12.53437749827671,0
80.28892262385639,12.43254149390298,0
80.28616076426671,12.38164079913633,0
80.26511197822781,12.31474736051312,0
80.26050279541035,12.22995820537765,0
80.23941074889372,12.16313653744558,0
80.21645688792,12.06243774013701,0
80.17619514615069,11.96267868889682,0
80.15607602451276,11.91279853066779,0
80.11770356329768,11.84691871805963,0
80.07750416467992,11.74715799351077,0
80.02187143303681,11.6821903960103,0
79.96351572639186,11.56640475126538,0
79.94065777203802,11.4657303741187,0
79.93510632394116,11.36414102440262,0
79.92860153755805,11.24562697520951,0
79.92393943340687,11.16097451362113,0
79.91740170713379,11.04245799454426,0
79.91646313955498,11.02552624900637,0


这篇关于如何获取XML标记的内部文本并将内部文本保存在文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆