嵌套 Foreach 在第一个循环后退出 - 写入 xml 元素

发布于 2025-01-17 08:06:01 字数 3448 浏览 2 评论 0原文

我正在尝试创建嵌套的XML元素,以如下所示,但是创建的文件仅包含headingTags的第一个元素,然后退出了第一个foreach循环。为什么会发生这种情况?

<?xml version="1.0"?>
<ProductProfile>
    <ProfileVersion>input</ProfileVersion>
        <ProductInformation>
            <ApplicableProducts>input</ApplicableProducts>
            <ProductDescription>input</ProductDescription>
        </ProductInformation>
        <CustomerInformation>
            <Name>input</Name>
        </CustomerInformation>
        <ProductionSettings>
            <LabelType>input</LabelType>
            <LabelQuantity>input</LabelQuantity>
            <Settings>
                <SettingsSRegisters>input</SettingsSRegisters>
            </Settings>
        </ProductionSettings>
        <PostProduction>
            <ActivationStatus>input</ActivationStatus>
        </PostProduction>
</ProductProfile>

自从我添加了第一个foreach之后的行tw.writeEndelement();以来,这才开始发生。没有这条线,所有元素的末尾都在文件的末尾。

我的代码:

 Dictionary<string, int> tagNames = new Dictionary<string, int>()
{
    {"ProfileVersion", 0},
    {"ApplicableProducts", 1},
    {"ProductDescription", 1},
    {"Name", 2},
    {"LabelType", 3},
    {"LabelQuantity", 3},
    {"SettingsSRegisters", 4},
    {"ActivationStatus", 5}
};
    
public enum headingTags
{
    ProductProfile,     //0
    ProductInformation, //1
    CustomerInformation,//2
    ProductionSettings, //3
    Settings,           //4
    PostProduction      //5
}
public void SaveProfile_Click(object sender, RoutedEventArgs e)
{
    using (XmlTextWriter tw = new XmlTextWriter(path, Encoding.UTF8))
    {
        tw.Formatting = Formatting.Indented;
        // Opens the document  
        tw.WriteStartDocument();
        //tw.WriteComment("Configuration File");

        foreach (int headingValue in Enum.GetValues(typeof(dataTags.headingTags)))
        {
            MessageBox.Show("Heading value does not match Tag name" + headingValue + " " );
            string headingString = Enum.GetName(typeof(dataTags.headingTags), headingValue);

            // Write heading element  
            tw.WriteStartElement(headingString);
            tw.WriteString(txtSettingsSRegisters.Text);
            foreach (KeyValuePair<string, int> dictLine in tagNames)
            {
                if (headingValue == dictLine.Value)
                {
                    tw.WriteStartElement(dictLine.Key, "");
                    tw.WriteString("TextBoxInput");
                    tw.WriteEndElement();
                    tw.WriteWhitespace("/n");
                }
                else
                {
                    //MessageBox.Show("Heading value does not match Tag name" + headingValue + " " + dictLine.Value);
                }
            }
            //tw.WriteEndElement();
        }
        tw.WriteEndDocument();
        // close writer  
        tw.Close();
    }
}

更新: 我添加了一个尝试捕获量以找到被抛出的异常错误:State Epilog中的令牌启动将导致无效的XML文档。,并在foreach之后添加了此行(int headingValue enum in enum。 getValues(typeof(datatags.headingtags)))以实现正确的格式。

if (j != 0) { 
                            tw.WriteEndElement();
                        }

I am trying to create nested xml elements to look like below however the created file only contains the first element from headingTags and then the first foreach loop is exited. Why is this happening?

<?xml version="1.0"?>
<ProductProfile>
    <ProfileVersion>input</ProfileVersion>
        <ProductInformation>
            <ApplicableProducts>input</ApplicableProducts>
            <ProductDescription>input</ProductDescription>
        </ProductInformation>
        <CustomerInformation>
            <Name>input</Name>
        </CustomerInformation>
        <ProductionSettings>
            <LabelType>input</LabelType>
            <LabelQuantity>input</LabelQuantity>
            <Settings>
                <SettingsSRegisters>input</SettingsSRegisters>
            </Settings>
        </ProductionSettings>
        <PostProduction>
            <ActivationStatus>input</ActivationStatus>
        </PostProduction>
</ProductProfile>

This has only started happening since I added the line tw.WriteEndElement(); after the first foreach. Without this line all of the end of elements were at the end of the file.

My code:

 Dictionary<string, int> tagNames = new Dictionary<string, int>()
{
    {"ProfileVersion", 0},
    {"ApplicableProducts", 1},
    {"ProductDescription", 1},
    {"Name", 2},
    {"LabelType", 3},
    {"LabelQuantity", 3},
    {"SettingsSRegisters", 4},
    {"ActivationStatus", 5}
};
    
public enum headingTags
{
    ProductProfile,     //0
    ProductInformation, //1
    CustomerInformation,//2
    ProductionSettings, //3
    Settings,           //4
    PostProduction      //5
}
public void SaveProfile_Click(object sender, RoutedEventArgs e)
{
    using (XmlTextWriter tw = new XmlTextWriter(path, Encoding.UTF8))
    {
        tw.Formatting = Formatting.Indented;
        // Opens the document  
        tw.WriteStartDocument();
        //tw.WriteComment("Configuration File");

        foreach (int headingValue in Enum.GetValues(typeof(dataTags.headingTags)))
        {
            MessageBox.Show("Heading value does not match Tag name" + headingValue + " " );
            string headingString = Enum.GetName(typeof(dataTags.headingTags), headingValue);

            // Write heading element  
            tw.WriteStartElement(headingString);
            tw.WriteString(txtSettingsSRegisters.Text);
            foreach (KeyValuePair<string, int> dictLine in tagNames)
            {
                if (headingValue == dictLine.Value)
                {
                    tw.WriteStartElement(dictLine.Key, "");
                    tw.WriteString("TextBoxInput");
                    tw.WriteEndElement();
                    tw.WriteWhitespace("/n");
                }
                else
                {
                    //MessageBox.Show("Heading value does not match Tag name" + headingValue + " " + dictLine.Value);
                }
            }
            //tw.WriteEndElement();
        }
        tw.WriteEndDocument();
        // close writer  
        tw.Close();
    }
}

UPDATE:
I added a try catch to find the exception being thrown Error:Token StartElement in state Epilog would result in an invalid XML document. and just added this line after the foreach (int headingValue in Enum.GetValues(typeof(dataTags.headingTags))) to implement the correct format.

if (j != 0) { 
                            tw.WriteEndElement();
                        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文