jack carlson jack carlson - 3 years ago 185
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="" productVersion="15.0.0" PIVersion="" 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: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:Name>Smith Johnson</my:Name>
<my:Address>203 RealPlace Lane</my:Address>
<my:Market_Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">10</my:Market_Value>
<my:Number_Of_Dependents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">52</my:Number_Of_Dependents>
<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

Place_of_Work: Not abducting People Industries
Salary: 10
Start_Date: 1952-01-10 (ignore T00 its an unrelated bug )
Personal Information
Smith Johnson
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) + ":";
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(" ");
foreach (XmlAttribute attribute in node.Attributes)
sb.Append(" " + attribute.Name + ' ');

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

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.


^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(" ");
foreach (XmlNode attribute in node.ChildNodes)
sb.Append(" " + attribute.Name + ':');

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