【问题标题】:XML class not recognizing its attributesXML 类无法识别其属性
【发布时间】:2017-10-01 21:53:08
【问题描述】:

请随意将我链接到某事,我找不到其他类似的问题,但我确定我没有正确地提出问题

我有这个 XML 代码

<?xml version="1.0" encoding="UTF-8"?><?mso-infoPathSolution solutionVersion="1.0.0.2" productVersion="15.0.0" PIVersion="1.0.0.0" href="file:///C:\Users\Derek\Desktop\templates\TestForm.xsn" name="urn:schemas-microsoft-com:office:infopath:TestForm:-myXSD-2017-10-01T20-35-41" ?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?><my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2017-10-01T20:35:41" xml:lang="en-us">
<my:Occupation>
    <my:Place_of_Work>Not Abducting People Industries</my:Place_of_Work>
    <my:Salary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">10</my:Salary>
    <my:Start_Date xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1952-01-10T00:00:00</my:Start_Date>
</my:Occupation>
<my:Personal_Information>
    <my:Name>Smith Johnson</my:Name>
    <my:Number>123-456-7890</my:Number>
    <my:Home_Information>
        <my:Address>203 RealPlace Lane</my:Address>
        <my:Market_Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">10</my:Market_Value>
        <my:Mortage_Paid_Off>false</my:Mortage_Paid_Off>
    </my:Home_Information>
    <my:Number_Of_Dependents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">52</my:Number_Of_Dependents>
</my:Personal_Information>
<my:Description_of_Canidate>Candidate is a small man, requested loan for a bed and breakfeast startup.</my:Description_of_Canidate>

我想将此 XML 代码格式化为一个嵌套的 .txt 文件,该文件仅包含变量的名称及其值,如下所示

Occupation 
  Place_of_Work: Not abducting People Industries
  Salary: 10 
  Start_Date: 1952-01-10 (ignore T00 its an unrelated bug )
Personal Information
 Smith Johnson
 123-456-7809
 Home Information
  Address: 203 RealPlace Lane
  Market_Value: 10
  Mortage_Paid_Off: false
Description of candidate: Candidate is a small man, requested loan for a bed and breakfeast startup.

这是我当前的代码

                int count = Regex.Matches(line1, "xmlns").Count;
            String temp = line1;
            List<String> namespaces = new List<string>();
            int pFrom = 0;
            int pTo = 0;
            Boolean offset = false;
            int offsetter = 0;
            while (count >= 1)
            {
                temp = line1;
                pFrom = temp.IndexOf("xmlns", pFrom + offsetter) + "key : ".Length;
                pTo = temp.IndexOf("=", pFrom + offsetter);

                String result = line1.Substring(pFrom, pTo - pFrom) + ":";
                namespaces.Add(result);
                count--;
                offset = true;
                if (offset)
                {
                    offsetter = 1;
                }
            }
            StringBuilder sb = new StringBuilder();
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                //don't include ugly attachment string
                if (node.Name != "my:File_Attachment")
                {
                    //sb.Append(char.ToUpper(node.Name[0]));
                    sb.Append(node.Name);
                    sb.Append(" ");
                    sb.AppendLine(node.InnerText);
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        sb.Append(" " + attribute.Name + ' ');
                        sb.AppendLine(attribute.Value);

                    }
                }
            }
            //remove any namespaces in the XML
            foreach (String NS in namespaces)
            {
                sb.Replace(NS, "");
                String temp1 = FirstCharToUpper(NS);
                sb.Replace(temp1, "");
            }
            Console.WriteLine(sb);

第一部分可能看起来令人困惑,我所做的只是抓取文件中的任何命名空间(my: 事物)并将它们存储起来,以便以后将它们撕掉。在我创建字符串生成器之后就是这里真正相关的行

该循环应该找到任何节点,打印它们的名称,然后使用内部循环来打印它们内部属性的值。问题是这个节点似乎没有任何属性,它的内部文本有工作地点、工资和开始日期的信息。

https://gyazo.com/23fd9561b05af152d5487328811611d1

^这就是节点在调试时的样子

我在这里错过了什么?有没有更好的方法来解析这个?

编辑:解决了这个问题,似乎效果很好!如果您有任何建议,请告诉我

StringBuilder sb = new StringBuilder();
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                //don't include ugly attachment string
                if (node.Name != "my:File_Attachment")
                {
                    //sb.Append(char.ToUpper(node.Name[0]));
                    sb.AppendLine(node.Name);
                    sb.Append(" ");
                    //sb.AppendLine();
                    //sb.AppendLine(node.InnerText);
                    foreach (XmlNode attribute in node.ChildNodes)
                    {
                        sb.Append(" " + attribute.Name + ':');
                        sb.AppendLine(attribute.InnerText);
                        //sb.AppendLine();
                    }
                }
            } 

【问题讨论】:

    标签: c# xml linq parsing


    【解决方案1】:

    属性就是这样的东西:

    <AgeNode Born="sadf" Salary="asdf" .../> 
    

    你必须调查child nodes ...我猜

    【讨论】:

    • 检查我的编辑以获得我的解决方案!还有,为什么这么冷漠的朋友?我们在编程!!! =) =) = ) !!!
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    相关资源
    最近更新 更多