遍历一个xml并输出指定节点的值

liysky 2010-02-23 11:34:56
我有一个类似的xml的 string,想通过遍历怎么个xml 输出我想要的element的值

xml 为:

<Discover xmlns="urn:schemas-microsoft-com:xml-analysis">
<RequestType>DISCOVER_XML_METADATA</RequestType>
<Restrictions>
<RestrictionList>
<ObjectExpansion>ObjectProperties</ObjectExpansion>
</RestrictionList>
</Restrictions>
<Properties>
<PropertyList>
</PropertyList>
</Properties>
</Discover>


我现在通过遍历,先判断是否有 ObjectExpansion 节点,如果有输出其值。

请指教。
...全文
236 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jnshijin 2010-02-24
  • 打赏
  • 举报
回复
谢谢楼主的提问,找了很久了。
清风六月 2010-02-24
  • 打赏
  • 举报
回复
使用XPath,这个用起来很方便的.
zgke 2010-02-24
  • 打赏
  • 举报
回复
使用 SelectNodes
string _Xml = @"<Discover xmlns=""urn:schemas-microsoft-com:xml-analysis"">
<RequestType>DISCOVER_XML_METADATA </RequestType>
<Restrictions>
<RestrictionList>
<ObjectExpansion>ObjectProperties </ObjectExpansion>
</RestrictionList>
</Restrictions>
<Properties>
<PropertyList>
</PropertyList>
</Properties>
</Discover>";

XmlDocument _Documnet = new XmlDocument();
_Documnet.LoadXml(_Xml);

XmlNamespaceManager _Manager = new XmlNamespaceManager(_Documnet.NameTable);
_Manager.AddNamespace("XML", "urn:schemas-microsoft-com:xml-analysis");


XmlNodeList _List = _Documnet.SelectNodes("//XML:ObjectExpansion", _Manager);


birdwawe 2010-02-24
  • 打赏
  • 举报
回复
看着代码就头疼,呵呵,真是,无奈的人生.
zyhnk 2010-02-23
  • 打赏
  • 举报
回复
/// <summary>
/// 获取节点的值
/// </summary>
/// <param name="xmlPath">文件路径+文件名</param>
/// <param name="strAppKey">键名</param>
/// <returns></returns>
public string GetXmlNodeValue(string xmlPath, string strAppKey)
{
XmlNode xNode;
string strResult = "";
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(xmlPath);
if (xDoc.HasChildNodes)
{
for (int i = 0; i < xDoc.ChildNodes.Count; i++)
{
xNode = xDoc.ChildNodes[i];
if (xNode.Name == strAppKey)
{
strResult = xNode.InnerText;
break;

}
else
{
if (xNode.HasChildNodes)
{
strResult = GetXmlNodeValue(xNode, strAppKey);
if (strResult != "")
{
break;
}
}
}
}

}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{ }
return strResult;
}
seesea125 2010-02-23
  • 打赏
  • 举报
回复
/// <summary>
/// 根据节点名读出相应的节点内容
/// </summary>
/// <param name="parentname"></param>
/// <param name="subname"></param>
/// <returns></returns>
public static string GetNode(string filepath,string parentname,string subname)
{
string _NodeValue=string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Config_Cache").ChildNodes;

foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == parentname)
{
XmlNodeList nls = xe.ChildNodes;
foreach (XmlNode xnl in nls)
{
XmlElement xe2 = (XmlElement)xnl;
if (xe2.Name.ToLower() == subname.ToLower())
{
_NodeValue = xe2.InnerXml;
break;
}
}
}
}
return _NodeValue;
}
/// <summary>
/// 将xml文件转换为DataSet
/// </summary>
/// <param name="xmlFile"></param>
/// <returns></returns>
public DataSet ConvertXMLFileToDataSet(string xmlFile)
{
StringReader stream = null;
XmlTextReader reader = null;
DataSet xmlDS = null;
try
{

lock (this)
{
XmlDocument xmld = new XmlDocument();
xmld.Load(xmlFile);
stream = new StringReader(xmld.InnerXml);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
}
}
//catch (System.Exception ex)
catch
{
//throw ex;
}
finally
{
if (reader != null)
reader.Close();
}

return xmlDS;
}
/// <summary>
/// 将DataSet转换为xml文件
/// </summary>
/// <param name="xmlDS"></param>
/// <param name="xmlFile"></param>
public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
{
MemoryStream stream = null;
XmlTextWriter writer = null;

try
{
stream = new MemoryStream();
//从stream装载到XmlTextWriter
writer = new XmlTextWriter(stream, Encoding.Unicode);

//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);

//返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
if (!System.IO.File.Exists(xmlFile))
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
}
sw.Close();
}
catch (System.Exception ex)
{
//throw ex;
}
finally
{
if (writer != null)
writer.Close();
}
}
lzsh0622 2010-02-23
  • 打赏
  • 举报
回复
// XML 读取XML文件中的元素和元素属性
private static void PrintElement(XmlDocument document)
{
XmlNodeList nodeList = document.GetElementsByTagName("*"); //获取所有的Node
for (int i = 0; i < nodeList.Count; i++)
{
XmlNode node = nodeList.Item(i);
MessageBox.Show(node.Name); //打印每一个node的名称
}
}

private static void PrintAttributes(XmlDocument document)
{
XmlNodeList nodeList = document.GetElementsByTagName("*");
XmlNamedNodeMap nameNodeMap;
XmlElement element;
XmlAttribute attribute;
string attributeName;
string attributeValue;

for (int i = 0; i < nodeList.Count; i++)
{
element = (XmlElement)nodeList.Item(i);
MessageBox.Show(element.Name + ":" + element.ChildNodes[0].Value);
nameNodeMap = element.Attributes;
if (nameNodeMap != null)
{
for (int j = 0; j < nameNodeMap.Count; j++)
{
attribute = (XmlAttribute)nameNodeMap.Item(j);
attributeName = attribute.Name;
attributeValue = attribute.Value;
MessageBox.Show("属性是:" + attributeName + "=" + attributeValue);
}
}
}
}

// 测试用例
private void button4_Click(object sender, EventArgs e)
{
XmlDocument document = new XmlDocument();
document.Load("c:\\student.xml");
Console.WriteLine("元素是:");
PrintElement(document);

Console.WriteLine("元素属性是:");
PrintAttributes(document);
}

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧