Parsing XML String Returned from WebRequest
I have an xml string returned from a HTTPWebRequest which I need to parse through to obtain the correct mailing address. The tag contains a child tag of which will either be Primary or Alternate. If the tag equals Alternate I will use the address values for that CustAddr, but if there is no Alternate then I will use the primary as the default. In C# how would I do this using the System.Xml.Linq library?
<Customer>
<CustInfo>
<CustAddr>
<FullName>MY PRIMARY NAME</FullName>
<Addr1>PRIMARY ADDRESS1</Addr1>
<Addr2>PRIMARY ADDRESS2</Addr2>
<Addr3>PRIMARY ADDRESS3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>PRIMARY</AddrCode>
</CustAddr>
<CustAddr>
<FullName>MY ALTERNATE ADDRESS LINE 1</FullName>
<Addr1>TEST ALT ADDRESS LINE1</Addr1>
<Addr2>TEST ALT ADDRESS LINE2</Addr2>
<Addr3>TEST ALT ADDRESS LINE3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>ALTERNATE</AddrCode>
</CustAddr>
</CustInfo>
</Customer>
c# xml parsing
add a comment |
I have an xml string returned from a HTTPWebRequest which I need to parse through to obtain the correct mailing address. The tag contains a child tag of which will either be Primary or Alternate. If the tag equals Alternate I will use the address values for that CustAddr, but if there is no Alternate then I will use the primary as the default. In C# how would I do this using the System.Xml.Linq library?
<Customer>
<CustInfo>
<CustAddr>
<FullName>MY PRIMARY NAME</FullName>
<Addr1>PRIMARY ADDRESS1</Addr1>
<Addr2>PRIMARY ADDRESS2</Addr2>
<Addr3>PRIMARY ADDRESS3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>PRIMARY</AddrCode>
</CustAddr>
<CustAddr>
<FullName>MY ALTERNATE ADDRESS LINE 1</FullName>
<Addr1>TEST ALT ADDRESS LINE1</Addr1>
<Addr2>TEST ALT ADDRESS LINE2</Addr2>
<Addr3>TEST ALT ADDRESS LINE3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>ALTERNATE</AddrCode>
</CustAddr>
</CustInfo>
</Customer>
c# xml parsing
explain better the issue here
– oetoni
Nov 20 at 23:49
add a comment |
I have an xml string returned from a HTTPWebRequest which I need to parse through to obtain the correct mailing address. The tag contains a child tag of which will either be Primary or Alternate. If the tag equals Alternate I will use the address values for that CustAddr, but if there is no Alternate then I will use the primary as the default. In C# how would I do this using the System.Xml.Linq library?
<Customer>
<CustInfo>
<CustAddr>
<FullName>MY PRIMARY NAME</FullName>
<Addr1>PRIMARY ADDRESS1</Addr1>
<Addr2>PRIMARY ADDRESS2</Addr2>
<Addr3>PRIMARY ADDRESS3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>PRIMARY</AddrCode>
</CustAddr>
<CustAddr>
<FullName>MY ALTERNATE ADDRESS LINE 1</FullName>
<Addr1>TEST ALT ADDRESS LINE1</Addr1>
<Addr2>TEST ALT ADDRESS LINE2</Addr2>
<Addr3>TEST ALT ADDRESS LINE3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>ALTERNATE</AddrCode>
</CustAddr>
</CustInfo>
</Customer>
c# xml parsing
I have an xml string returned from a HTTPWebRequest which I need to parse through to obtain the correct mailing address. The tag contains a child tag of which will either be Primary or Alternate. If the tag equals Alternate I will use the address values for that CustAddr, but if there is no Alternate then I will use the primary as the default. In C# how would I do this using the System.Xml.Linq library?
<Customer>
<CustInfo>
<CustAddr>
<FullName>MY PRIMARY NAME</FullName>
<Addr1>PRIMARY ADDRESS1</Addr1>
<Addr2>PRIMARY ADDRESS2</Addr2>
<Addr3>PRIMARY ADDRESS3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>PRIMARY</AddrCode>
</CustAddr>
<CustAddr>
<FullName>MY ALTERNATE ADDRESS LINE 1</FullName>
<Addr1>TEST ALT ADDRESS LINE1</Addr1>
<Addr2>TEST ALT ADDRESS LINE2</Addr2>
<Addr3>TEST ALT ADDRESS LINE3</Addr3>
<Addr4></Addr4>
<Addr5></Addr5>
<AddrCode>ALTERNATE</AddrCode>
</CustAddr>
</CustInfo>
</Customer>
c# xml parsing
c# xml parsing
asked Nov 20 at 22:32
Workflow_Automation
1
1
explain better the issue here
– oetoni
Nov 20 at 23:49
add a comment |
explain better the issue here
– oetoni
Nov 20 at 23:49
explain better the issue here
– oetoni
Nov 20 at 23:49
explain better the issue here
– oetoni
Nov 20 at 23:49
add a comment |
1 Answer
1
active
oldest
votes
Try following xml linq. I used Regex to get addresses. Needed to avoid getting AddrCode as part of the address.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string args)
{
string xml = File.ReadAllText(FILENAME);
//use parse to take string returned from request
XDocument doc = XDocument.Parse(xml);
KeyValuePair<string, List<string>> results = doc.Descendants("CustAddr").Where(x => (string)x.Element("AddrCode") == "PRIMARY")
.Select(x => new KeyValuePair<string, List<string>>((string)x.Element("FullName"), x.Elements().Where(y => Regex.IsMatch(y.Name.LocalName, @"Addrd")).Select(y => (string)y).ToList())).FirstOrDefault();
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402589%2fparsing-xml-string-returned-from-webrequest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try following xml linq. I used Regex to get addresses. Needed to avoid getting AddrCode as part of the address.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string args)
{
string xml = File.ReadAllText(FILENAME);
//use parse to take string returned from request
XDocument doc = XDocument.Parse(xml);
KeyValuePair<string, List<string>> results = doc.Descendants("CustAddr").Where(x => (string)x.Element("AddrCode") == "PRIMARY")
.Select(x => new KeyValuePair<string, List<string>>((string)x.Element("FullName"), x.Elements().Where(y => Regex.IsMatch(y.Name.LocalName, @"Addrd")).Select(y => (string)y).ToList())).FirstOrDefault();
}
}
}
add a comment |
Try following xml linq. I used Regex to get addresses. Needed to avoid getting AddrCode as part of the address.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string args)
{
string xml = File.ReadAllText(FILENAME);
//use parse to take string returned from request
XDocument doc = XDocument.Parse(xml);
KeyValuePair<string, List<string>> results = doc.Descendants("CustAddr").Where(x => (string)x.Element("AddrCode") == "PRIMARY")
.Select(x => new KeyValuePair<string, List<string>>((string)x.Element("FullName"), x.Elements().Where(y => Regex.IsMatch(y.Name.LocalName, @"Addrd")).Select(y => (string)y).ToList())).FirstOrDefault();
}
}
}
add a comment |
Try following xml linq. I used Regex to get addresses. Needed to avoid getting AddrCode as part of the address.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string args)
{
string xml = File.ReadAllText(FILENAME);
//use parse to take string returned from request
XDocument doc = XDocument.Parse(xml);
KeyValuePair<string, List<string>> results = doc.Descendants("CustAddr").Where(x => (string)x.Element("AddrCode") == "PRIMARY")
.Select(x => new KeyValuePair<string, List<string>>((string)x.Element("FullName"), x.Elements().Where(y => Regex.IsMatch(y.Name.LocalName, @"Addrd")).Select(y => (string)y).ToList())).FirstOrDefault();
}
}
}
Try following xml linq. I used Regex to get addresses. Needed to avoid getting AddrCode as part of the address.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string args)
{
string xml = File.ReadAllText(FILENAME);
//use parse to take string returned from request
XDocument doc = XDocument.Parse(xml);
KeyValuePair<string, List<string>> results = doc.Descendants("CustAddr").Where(x => (string)x.Element("AddrCode") == "PRIMARY")
.Select(x => new KeyValuePair<string, List<string>>((string)x.Element("FullName"), x.Elements().Where(y => Regex.IsMatch(y.Name.LocalName, @"Addrd")).Select(y => (string)y).ToList())).FirstOrDefault();
}
}
}
edited Nov 21 at 2:28
answered Nov 21 at 2:22
jdweng
16.7k2717
16.7k2717
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402589%2fparsing-xml-string-returned-from-webrequest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
explain better the issue here
– oetoni
Nov 20 at 23:49