jack carlson jack carlson - 3 years ago 167
C# Question

XML class not recognizing its attributes

Feel free to link me to something, I wasn't able to find another question like this but I'm sure I'm not framing the question correctly

I have this XML code

<?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>




I want to format this XML code into a nested .txt file that just has the names of the variables and their values, like this

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.


This is my current code

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


The first part might seem confusing, all I'm doing is grabbing any namespaces in the file (the my: things) and storing them so I can rip them out later. Right after I make the stringbuilder is the line that's really relevant here

The loop is supposed to find any nodes, print their names, then use an inner loop to print the values of their internal attributes. The issue is that this node doesn't seem to have any attributes, its inner text has the information for place of work, salary, and start date.

https://gyazo.com/23fd9561b05af152d5487328811611d1

^That's what the node looks like at debug time

What have I missed here? Is there a better way to parse this?

EDIT: Solved it with this, seems to work well! Please let me know if you have any suggestions

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

Answer Source

Attributes are such things:

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

you have to look into child nodes ... i guess

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download