需要帮助调试c#XML-parsing

时间:2022-08-26 11:28:52

my c# code makes VisualStudio (2013) not respond while using the whole XMLfile (189 descriptors having 64 features each), but on a small amount works well

我的c#代码使VisualStudio(2013)在使用整个XML文件时没有响应(189个描述符各有64个功能),但是少量工作得很好

        Matrix<float> ObjectDescriptors = new Matrix<float>(200, 64); 
        XmlTextReader reader = new XmlTextReader("descriptors.xml");
        int i = -1;
        int ii = 0;
        while (reader.Read())
        {
            if (reader.Name == "feature" && ii < 64)
            {                   
                ObjectDescriptors[i, ii] = reader.ReadElementContentAsFloat();

                ii++;
                if (ii == 64) ii = 0;
            }
            else if (reader.Name == "descriptor") i++;
        }

The XML data is clean. Has anyone ideas why the time increases non linearly while the samples number increases? Or the problem is elsewhere?

XML数据很干净。有没有人想到为什么时间在样本数量增加时非线性增加?或者问题出在其他地方?

Thank you!

2 个解决方案

#1


0  

Do you have any exception handling?

你有任何异常处理吗?

Expression: else if (reader.Name == "descriptor") i++; must lead to increment i as well start element descriptor, as end element.

表达式:else if(reader.Name ==“descriptor”)i ++;必须导致增加i以及开始元素描述符,作为结束元素。

In my test code, this leads to IndexOutOfRangeException. Perhaps your code hangs on exception handling?

在我的测试代码中,这会导致IndexOutOfRangeException。也许你的代码挂起了异常处理?

Try this: else if (reader.NodeType == XmlNodeType.Element && reader.Name == "descriptor") i++;

试试这个:else if(reader.NodeType == XmlNodeType.Element && reader.Name ==“descriptor”)i ++;

Moreover, I can offer two more ways.

而且,我还可以提供两种方式。

Use NameTable. This may improve performance. However, the number of nodes 189 * 64 is not very large. The difference is almost negligible.

使用NameTable。这可以提高性能。然而,节点189 * 64的数量不是很大。差异几乎可以忽略不计。

NameTable nt = new NameTable();
object feature = nt.Add("feature");
object descriptor = nt.Add("descriptor");

var settings = new XmlReaderSettings();
settings.NameTable = nt;

using (var reader = XmlReader.Create("descriptors.xml", settings))
{
    int i = -1;
    int ii = 0;

    while (reader.Read())
    {
        if (object.ReferenceEquals(feature, reader.Name))
        {
            ObjectDescriptors[i, ii] = reader.ReadElementContentAsFloat();
            ii++;
            if (ii == 64) ii = 0;
        }
        else if (reader.NodeType == XmlNodeType.Element && object.ReferenceEquals(descriptor, reader.Name)) i++;
    }
}

Another way:

using (var reader = XmlReader.Create("descriptors.xml"))
{
    while (reader.Read())
    {
        if (reader.ReadToFollowing("descriptor"))
        {
            int i = -1;
            do
            {
                i++;
                if (reader.ReadToFollowing("feature"))
                {
                    int ii = 0;
                    do
                    {
                        ObjectDescriptors[i, ii] = reader.ReadElementContentAsFloat();
                        ii++;
                    } while (reader.ReadToNextSibling("feature"));
                }
            } while (reader.ReadToNextSibling("descriptor"));
        }
    }
}

I used your ugly indexing starting on -1.

我用你的丑陋索引从-1开始。

Also note that XmlTextReader is not recommended long ago. Create reader using XmlReader.Create.

另请注意,很久以前不建议使用XmlTextReader。使用XmlReader.Create创建阅读器。

PS: что ж ты на русском не спросил? пришлось долго словарь и переводчик мучить.

PS:чтожтынарусскомнеспросил? пришлосьдолгословарьипереводчикмучить。

#2


0  

If it is the XML parser, you may try another approach such as

如果是XML解析器,您可以尝试其他方法,例如

float[,] data = new Float[200,64];
int row = 0, col = 0;
var tags = XElement.Parse(file).XPathSelectElements("descriptor|feature");

foreach(var node in tags)
{
   if (node.Name == "descriptor")
   { row++; col = 0; }
   else if (node.Name == "feature")
   { data[row, col++] = (float)node; }
}

#1


0  

Do you have any exception handling?

你有任何异常处理吗?

Expression: else if (reader.Name == "descriptor") i++; must lead to increment i as well start element descriptor, as end element.

表达式:else if(reader.Name ==“descriptor”)i ++;必须导致增加i以及开始元素描述符,作为结束元素。

In my test code, this leads to IndexOutOfRangeException. Perhaps your code hangs on exception handling?

在我的测试代码中,这会导致IndexOutOfRangeException。也许你的代码挂起了异常处理?

Try this: else if (reader.NodeType == XmlNodeType.Element && reader.Name == "descriptor") i++;

试试这个:else if(reader.NodeType == XmlNodeType.Element && reader.Name ==“descriptor”)i ++;

Moreover, I can offer two more ways.

而且,我还可以提供两种方式。

Use NameTable. This may improve performance. However, the number of nodes 189 * 64 is not very large. The difference is almost negligible.

使用NameTable。这可以提高性能。然而,节点189 * 64的数量不是很大。差异几乎可以忽略不计。

NameTable nt = new NameTable();
object feature = nt.Add("feature");
object descriptor = nt.Add("descriptor");

var settings = new XmlReaderSettings();
settings.NameTable = nt;

using (var reader = XmlReader.Create("descriptors.xml", settings))
{
    int i = -1;
    int ii = 0;

    while (reader.Read())
    {
        if (object.ReferenceEquals(feature, reader.Name))
        {
            ObjectDescriptors[i, ii] = reader.ReadElementContentAsFloat();
            ii++;
            if (ii == 64) ii = 0;
        }
        else if (reader.NodeType == XmlNodeType.Element && object.ReferenceEquals(descriptor, reader.Name)) i++;
    }
}

Another way:

using (var reader = XmlReader.Create("descriptors.xml"))
{
    while (reader.Read())
    {
        if (reader.ReadToFollowing("descriptor"))
        {
            int i = -1;
            do
            {
                i++;
                if (reader.ReadToFollowing("feature"))
                {
                    int ii = 0;
                    do
                    {
                        ObjectDescriptors[i, ii] = reader.ReadElementContentAsFloat();
                        ii++;
                    } while (reader.ReadToNextSibling("feature"));
                }
            } while (reader.ReadToNextSibling("descriptor"));
        }
    }
}

I used your ugly indexing starting on -1.

我用你的丑陋索引从-1开始。

Also note that XmlTextReader is not recommended long ago. Create reader using XmlReader.Create.

另请注意,很久以前不建议使用XmlTextReader。使用XmlReader.Create创建阅读器。

PS: что ж ты на русском не спросил? пришлось долго словарь и переводчик мучить.

PS:чтожтынарусскомнеспросил? пришлосьдолгословарьипереводчикмучить。

#2


0  

If it is the XML parser, you may try another approach such as

如果是XML解析器,您可以尝试其他方法,例如

float[,] data = new Float[200,64];
int row = 0, col = 0;
var tags = XElement.Parse(file).XPathSelectElements("descriptor|feature");

foreach(var node in tags)
{
   if (node.Name == "descriptor")
   { row++; col = 0; }
   else if (node.Name == "feature")
   { data[row, col++] = (float)node; }
}