-1

I'm having trouble iterating through a xml string in C#. Looked everywhere online but the examples load an actual .xml file whereas I'm trying to just iterate the string.

I have the following method that returns XML as a string

public static string WRequest(string URL, string method, string postData)
{}

In another method, I want to take the result and iterate the string.

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);
    }
}

The results come back all as one line.. whereas I want to iterate through each record and the record child nodes.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
user5120455
  • 141
  • 2
  • 4
  • 15
  • 1
    http://stackoverflow.com/q/55828/62576 – Ken White Oct 09 '15 at 17:36
  • 2
    Stop trying to parse XML as a string. Use a DOM parser where you can easily work with nodes and iterate through the XML structure. – Ken White Oct 09 '15 at 17:37
  • Appreciate the vague answers. When i was loading the string atleast i was able to get the data, now using the XmlDocument I'm not getting anything, hence why I was using the string in the first place. These are not static .xml files, I'm running queries to a filemaker database which returns an XML response – user5120455 Oct 09 '15 at 18:46
  • This line makes no sense - `var root_desc = root.Descendants("{http://www.filemaker.com/xml/fmresultset}record");`. Is that really the name of an element in the XML? – Tim Oct 09 '15 at 19:03
  • In filemaker XML webpublishing, yes – user5120455 Oct 09 '15 at 19:06
  • @Tim it does make sense, it will be converted to an `XName` with a namespace `http://www.filemaker.com/xml/fmresultset` and local name `record`. – Charles Mager Oct 11 '15 at 07:51

2 Answers2

0

Your code will iterate all record elements and just write the value of the concatenated text in all child elements. If you want to iterate through each of the child nodes individually then you want something like:

Namespace fmr = "http://www.filemaker.com/xml/fmresultset"
var results = doc.Descendants(fmr + "record").Descendants();

That said, this probably isn't that useful. Presumably there is specific data within each record you want to get?

Charles Mager
  • 25,735
  • 2
  • 35
  • 45
-1

This is a recursive way to analize a bunch of XML files inside a folder.

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();
        }
    }
}
deathx91
  • 1
  • 2