XML类无法识别其属性

时间:2017-10-01 21:53:08

标签: c# xml linq parsing

随意把我联系到某事,我无法找到这样的另一个问题,但我确信我没有正确地构建问题

我有这个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);

第一部分可能看起来令人困惑,我所做的只是抓取文件中的任何名称空间(我的:东西)并存储它们以便我以后可以将它们删除。在我创建stringbuilder之后就是这里真正相关的行

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

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

1 个答案:

答案 0 :(得分:0)

属性是这样的:

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

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