在c#中迭代XML字符串
本文关键字:XML 字符串 迭代 | 更新日期: 2023-09-27 18:16:54
我在c#中迭代xml字符串时遇到了麻烦。在网上到处看,但例子加载一个实际的。xml文件,而我只是试图迭代字符串。
我有以下方法返回XML作为字符串
public static string WRequest(string URL, string method, string postData)
{}
在另一个方法中,我想获取结果并迭代字符串。
public void do_xmlIterate()
{
string result = WRequest(query, "GET", "");
//At this point I get the XML string back.
XDocument doc = XDocument.Parse(result);
var root=doc.Root;
var root_desc = root.Descendants("{http://www.filemaker.com/xml/fmresultset}record");
foreach(var item in root_desc)
{
Console.WriteLine(item.value);
}
}
结果返回为一行。而我想遍历每个记录和记录子节点。
您的代码将迭代所有record
元素,并只在所有子元素中写入连接文本的值。如果你想逐个遍历每个子节点那么你需要这样写:
Namespace fmr = "http://www.filemaker.com/xml/fmresultset"
var results = doc.Descendants(fmr + "record").Descendants();
也就是说,这可能不是那么有用。假设在您想要获取的每条记录中都有特定的数据?
这是一种递归分析文件夹内一堆XML文件的方法。
protected void btnProcessFiles_Click(object sender, EventArgs e)
{
objDAO.SqlExec("SP_CLEAN");
DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["XMLDir"]);
foreach (FileInfo fl in di.GetFiles())
{
XmlDocument doc = new XmlDocument();
doc.Load(ConfigurationManager.AppSettings["XMLDir"] + fl.Name);
string xml = doc.InnerXml;
byte[] encodedString = Encoding.UTF8.GetBytes(xml);
MemoryStream ms = new MemoryStream(encodedString);
ms.Flush();
ms.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);
XmlElement element = xmlDoc.DocumentElement;
XmlNodeList nodes = element.ChildNodes;
registerId = objDAO.SqlCall("SELECT NEWID()").Rows[0][0].ToString();
XMLElementAnalyzer(nodes, "1", objDAO.SqlCall("SP_ADD_VALUE '" + registerId + "','1','1',1,'00000000-0000-0000-0000-000000000000'").Rows[0][0].ToString(), 1);
registerId = String.Empty;
if (new FileInfo(ConfigurationManager.AppSettings["XMLDir"] + "Done''" + fl.Name).Exists)
{
new FileInfo(ConfigurationManager.AppSettings["XMLDir"] + "Done''" + fl.Name).Delete();
fl.MoveTo(ConfigurationManager.AppSettings["XMLDir"] + "Done''" + fl.Name);
}
else
{
fl.MoveTo(ConfigurationManager.AppSettings["XMLDir"] + "Done''" + fl.Name);
}
}
}
public void XMLElementAnalyzer(XmlNodeList nodes, String parent, String parentId, int instance)
{
String lastNode = String.Empty;
String id = String.Empty;
foreach (XmlNode node in nodes)
{
if (lastNode.Equals(String.Empty))
{
lastNode = node.Name;
}
else
{
instance += (node.Name.Equals(lastNode) ? 1 : 0);
}
String nodeId = String.Empty;
if (!node.Name.Equals("#text"))
{
dt = objDAO.SqlCall("SP_CHECK_NODE '" + node.Name + "','" + parent + "'");
if (dt.Rows.Count == 0)
{
nodeId = objDAO.SqlCall("SP_ADD_NODE '" + node.Name + "','" + parent + "'").Rows[0][0].ToString();
}
else
{
nodeId = dt.Rows[0][0].ToString();
}
}
if (node.Attributes != null && node.Attributes.Count > 0)
{
id = objDAO.SqlCall("SP_ADD_VALUE '" + registerId + "','" + nodeId + "','" + nodeId + "'," + instance.ToString() + ",'" + parentId + "'").Rows[0][0].ToString();
String attrId = String.Empty;
foreach (XmlAttribute attr in node.Attributes)
{
dt = objDAO.SqlCall("SP_CHECK_NODE '" + attr.Name + "','" + nodeId + "'");
if (dt.Rows.Count == 0)
{
attrId = objDAO.SqlCall("SP_ADD_NODE '" + attr.Name + "','" + nodeId + "'").Rows[0][0].ToString();
}
else
{
attrId = dt.Rows[0][0].ToString();
}
objDAO.SqlCall("SP_ADD_VALUE '" + registerId + "','" + attrId + "','" + attr.Value + "'," + instance.ToString() + ",'" + id + "'").Rows[0][0].ToString();
}
}
if (node.Name.Equals("#text"))
{
id = objDAO.SqlCall("SP_ADD_VALUE '" + registerId + "','" + parent + "','" + node.Value + "'," + instance.ToString() + ",'" + parentId + "'").Rows[0][0].ToString();
}
if (node.ChildNodes.Count > 0)
{
if (node.Attributes == null || node.Attributes.Count == 0)
id = objDAO.SqlCall("SP_ADD_VALUE '" + registerId + "','" + nodeId + "','" + nodeId + "'," + instance.ToString() + ",'" + parentId + "'").Rows[0][0].ToString();
XMLElementAnalyzer(node.ChildNodes, nodeId, id, instance);
}
if (node.InnerText.Equals("") && node.Attributes.Count == 0 && node.ChildNodes.Count == 0)
{
objDAO.SqlCall("SP_ADD_VALUE '" + registerId + "','" + nodeId + "','" + nodeId + "'," + instance.ToString() + ",'" + parentId + "'").Rows[0][0].ToString();
objDAO.SqlCall("SP_ADD_VALUE '" + registerId + "','" + nodeId + "',''," + instance.ToString() + ",'" + parentId + "'").Rows[0][0].ToString();
}
}
}